home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gcc_260.zip / gcc_260 / sched.c < prev    next >
C/C++ Source or Header  |  1994-06-14  |  149KB  |  4,885 lines

  1. /* Instruction scheduling pass.
  2.    Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
  3.    Contributed by Michael Tiemann (tiemann@cygnus.com)
  4.    Enhanced by, and currently maintained by, Jim Wilson (wilson@cygnus.com)
  5.  
  6. This file is part of GNU CC.
  7.  
  8. GNU CC is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2, or (at your option)
  11. any later version.
  12.  
  13. GNU CC is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with GNU CC; see the file COPYING.  If not, write to
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. /* Instruction scheduling pass.
  23.  
  24.    This pass implements list scheduling within basic blocks.  It is
  25.    run after flow analysis, but before register allocation.  The
  26.    scheduler works as follows:
  27.  
  28.    We compute insn priorities based on data dependencies.  Flow
  29.    analysis only creates a fraction of the data-dependencies we must
  30.    observe: namely, only those dependencies which the combiner can be
  31.    expected to use.  For this pass, we must therefore create the
  32.    remaining dependencies we need to observe: register dependencies,
  33.    memory dependencies, dependencies to keep function calls in order,
  34.    and the dependence between a conditional branch and the setting of
  35.    condition codes are all dealt with here.
  36.  
  37.    The scheduler first traverses the data flow graph, starting with
  38.    the last instruction, and proceeding to the first, assigning
  39.    values to insn_priority as it goes.  This sorts the instructions
  40.    topologically by data dependence.
  41.  
  42.    Once priorities have been established, we order the insns using
  43.    list scheduling.  This works as follows: starting with a list of
  44.    all the ready insns, and sorted according to priority number, we
  45.    schedule the insn from the end of the list by placing its
  46.    predecessors in the list according to their priority order.  We
  47.    consider this insn scheduled by setting the pointer to the "end" of
  48.    the list to point to the previous insn.  When an insn has no
  49.    predecessors, we either queue it until sufficient time has elapsed
  50.    or add it to the ready list.  As the instructions are scheduled or
  51.    when stalls are introduced, the queue advances and dumps insns into
  52.    the ready list.  When all insns down to the lowest priority have
  53.    been scheduled, the critical path of the basic block has been made
  54.    as short as possible.  The remaining insns are then scheduled in
  55.    remaining slots.
  56.  
  57.    Function unit conflicts are resolved during reverse list scheduling
  58.    by tracking the time when each insn is committed to the schedule
  59.    and from that, the time the function units it uses must be free.
  60.    As insns on the ready list are considered for scheduling, those
  61.    that would result in a blockage of the already committed insns are
  62.    queued until no blockage will result.  Among the remaining insns on
  63.    the ready list to be considered, the first one with the largest
  64.    potential for causing a subsequent blockage is chosen.
  65.  
  66.    The following list shows the order in which we want to break ties
  67.    among insns in the ready list:
  68.  
  69.     1.  choose insn with lowest conflict cost, ties broken by
  70.     2.  choose insn with the longest path to end of bb, ties broken by
  71.     3.  choose insn that kills the most registers, ties broken by
  72.     4.  choose insn that conflicts with the most ready insns, or finally
  73.     5.  choose insn with lowest UID.
  74.  
  75.    Memory references complicate matters.  Only if we can be certain
  76.    that memory references are not part of the data dependency graph
  77.    (via true, anti, or output dependence), can we move operations past
  78.    memory references.  To first approximation, reads can be done
  79.    independently, while writes introduce dependencies.  Better
  80.    approximations will yield fewer dependencies.
  81.  
  82.    Dependencies set up by memory references are treated in exactly the
  83.    same way as other dependencies, by using LOG_LINKS.
  84.  
  85.    Having optimized the critical path, we may have also unduly
  86.    extended the lifetimes of some registers.  If an operation requires
  87.    that constants be loaded into registers, it is certainly desirable
  88.    to load those constants as early as necessary, but no earlier.
  89.    I.e., it will not do to load up a bunch of registers at the
  90.    beginning of a basic block only to use them at the end, if they
  91.    could be loaded later, since this may result in excessive register
  92.    utilization.
  93.  
  94.    Note that since branches are never in basic blocks, but only end
  95.    basic blocks, this pass will not do any branch scheduling.  But
  96.    that is ok, since we can use GNU's delayed branch scheduling
  97.    pass to take care of this case.
  98.  
  99.    Also note that no further optimizations based on algebraic identities
  100.    are performed, so this pass would be a good one to perform instruction
  101.    splitting, such as breaking up a multiply instruction into shifts
  102.    and adds where that is profitable.
  103.  
  104.    Given the memory aliasing analysis that this pass should perform,
  105.    it should be possible to remove redundant stores to memory, and to
  106.    load values from registers instead of hitting memory.
  107.  
  108.    This pass must update information that subsequent passes expect to be
  109.    correct.  Namely: reg_n_refs, reg_n_sets, reg_n_deaths,
  110.    reg_n_calls_crossed, and reg_live_length.  Also, basic_block_head,
  111.    basic_block_end.
  112.  
  113.    The information in the line number notes is carefully retained by this
  114.    pass.  All other NOTE insns are grouped in their same relative order at
  115.    the beginning of basic blocks that have been scheduled.  */
  116.  
  117. #include <stdio.h>
  118. #include "config.h"
  119. #include "rtl.h"
  120. #include "basic-block.h"
  121. #include "regs.h"
  122. #include "hard-reg-set.h"
  123. #include "flags.h"
  124. #include "insn-config.h"
  125. #include "insn-attr.h"
  126.  
  127. #ifdef INSN_SCHEDULING
  128. /* Arrays set up by scheduling for the same respective purposes as
  129.    similar-named arrays set up by flow analysis.  We work with these
  130.    arrays during the scheduling pass so we can compare values against
  131.    unscheduled code.
  132.  
  133.    Values of these arrays are copied at the end of this pass into the
  134.    arrays set up by flow analysis.  */
  135. static short *sched_reg_n_deaths;
  136. static int *sched_reg_n_calls_crossed;
  137. static int *sched_reg_live_length;
  138.  
  139. /* Element N is the next insn that sets (hard or pseudo) register
  140.    N within the current basic block; or zero, if there is no
  141.    such insn.  Needed for new registers which may be introduced
  142.    by splitting insns.  */
  143. static rtx *reg_last_uses;
  144. static rtx *reg_last_sets;
  145. static regset reg_pending_sets;
  146. static int reg_pending_sets_all;
  147.  
  148. /* Vector indexed by INSN_UID giving the original ordering of the insns.  */
  149. static int *insn_luid;
  150. #define INSN_LUID(INSN) (insn_luid[INSN_UID (INSN)])
  151.  
  152. /* Vector indexed by INSN_UID giving each instruction a priority.  */
  153. static int *insn_priority;
  154. #define INSN_PRIORITY(INSN) (insn_priority[INSN_UID (INSN)])
  155.  
  156. static short *insn_costs;
  157. #define INSN_COST(INSN)    insn_costs[INSN_UID (INSN)]
  158.  
  159. /* Vector indexed by INSN_UID giving an encoding of the function units
  160.    used.  */
  161. static short *insn_units;
  162. #define INSN_UNIT(INSN)    insn_units[INSN_UID (INSN)]
  163.  
  164. /* Vector indexed by INSN_UID giving an encoding of the blockage range
  165.    function.  The unit and the range are encoded.  */
  166. static unsigned int *insn_blockage;
  167. #define INSN_BLOCKAGE(INSN) insn_blockage[INSN_UID (INSN)]
  168. #define UNIT_BITS 5
  169. #define BLOCKAGE_MASK ((1 << BLOCKAGE_BITS) - 1)
  170. #define ENCODE_BLOCKAGE(U,R)                \
  171.   ((((U) << UNIT_BITS) << BLOCKAGE_BITS            \
  172.     | MIN_BLOCKAGE_COST (R)) << BLOCKAGE_BITS        \
  173.    | MAX_BLOCKAGE_COST (R))
  174. #define UNIT_BLOCKED(B) ((B) >> (2 * BLOCKAGE_BITS))
  175. #define BLOCKAGE_RANGE(B) \
  176.   (((((B) >> BLOCKAGE_BITS) & BLOCKAGE_MASK) << (HOST_BITS_PER_INT / 2)) \
  177.    | (B) & BLOCKAGE_MASK)
  178.  
  179. /* Encodings of the `<name>_unit_blockage_range' function.  */
  180. #define MIN_BLOCKAGE_COST(R) ((R) >> (HOST_BITS_PER_INT / 2))
  181. #define MAX_BLOCKAGE_COST(R) ((R) & ((1 << (HOST_BITS_PER_INT / 2)) - 1))
  182.  
  183. #define DONE_PRIORITY    -1
  184. #define MAX_PRIORITY    0x7fffffff
  185. #define TAIL_PRIORITY    0x7ffffffe
  186. #define LAUNCH_PRIORITY    0x7f000001
  187. #define DONE_PRIORITY_P(INSN) (INSN_PRIORITY (INSN) < 0)
  188. #define LOW_PRIORITY_P(INSN) ((INSN_PRIORITY (INSN) & 0x7f000000) == 0)
  189.  
  190. /* Vector indexed by INSN_UID giving number of insns referring to this insn.  */
  191. static int *insn_ref_count;
  192. #define INSN_REF_COUNT(INSN) (insn_ref_count[INSN_UID (INSN)])
  193.  
  194. /* Vector indexed by INSN_UID giving line-number note in effect for each
  195.    insn.  For line-number notes, this indicates whether the note may be
  196.    reused.  */
  197. static rtx *line_note;
  198. #define LINE_NOTE(INSN) (line_note[INSN_UID (INSN)])
  199.  
  200. /* Vector indexed by basic block number giving the starting line-number
  201.    for each basic block.  */
  202. static rtx *line_note_head;
  203.  
  204. /* List of important notes we must keep around.  This is a pointer to the
  205.    last element in the list.  */
  206. static rtx note_list;
  207.  
  208. /* Regsets telling whether a given register is live or dead before the last
  209.    scheduled insn.  Must scan the instructions once before scheduling to
  210.    determine what registers are live or dead at the end of the block.  */
  211. static regset bb_dead_regs;
  212. static regset bb_live_regs;
  213.  
  214. /* Regset telling whether a given register is live after the insn currently
  215.    being scheduled.  Before processing an insn, this is equal to bb_live_regs
  216.    above.  This is used so that we can find registers that are newly born/dead
  217.    after processing an insn.  */
  218. static regset old_live_regs;
  219.  
  220. /* The chain of REG_DEAD notes.  REG_DEAD notes are removed from all insns
  221.    during the initial scan and reused later.  If there are not exactly as
  222.    many REG_DEAD notes in the post scheduled code as there were in the
  223.    prescheduled code then we trigger an abort because this indicates a bug.  */
  224. static rtx dead_notes;
  225.  
  226. /* Queues, etc.  */
  227.  
  228. /* An instruction is ready to be scheduled when all insns following it
  229.    have already been scheduled.  It is important to ensure that all
  230.    insns which use its result will not be executed until its result
  231.    has been computed.  An insn is maintained in one of four structures:
  232.  
  233.    (P) the "Pending" set of insns which cannot be scheduled until
  234.    their dependencies have been satisfied.
  235.    (Q) the "Queued" set of insns that can be scheduled when sufficient
  236.    time has passed.
  237.    (R) the "Ready" list of unscheduled, uncommitted insns.
  238.    (S) the "Scheduled" list of insns.
  239.  
  240.    Initially, all insns are either "Pending" or "Ready" depending on
  241.    whether their dependencies are satisfied.
  242.  
  243.    Insns move from the "Ready" list to the "Scheduled" list as they
  244.    are committed to the schedule.  As this occurs, the insns in the
  245.    "Pending" list have their dependencies satisfied and move to either
  246.    the "Ready" list or the "Queued" set depending on whether
  247.    sufficient time has passed to make them ready.  As time passes,
  248.    insns move from the "Queued" set to the "Ready" list.  Insns may
  249.    move from the "Ready" list to the "Queued" set if they are blocked
  250.    due to a function unit conflict.
  251.  
  252.    The "Pending" list (P) are the insns in the LOG_LINKS of the unscheduled
  253.    insns, i.e., those that are ready, queued, and pending.
  254.    The "Queued" set (Q) is implemented by the variable `insn_queue'.
  255.    The "Ready" list (R) is implemented by the variables `ready' and
  256.    `n_ready'.
  257.    The "Scheduled" list (S) is the new insn chain built by this pass.
  258.  
  259.    The transition (R->S) is implemented in the scheduling loop in
  260.    `schedule_block' when the best insn to schedule is chosen.
  261.    The transition (R->Q) is implemented in `schedule_select' when an
  262.    insn is found to to have a function unit conflict with the already
  263.    committed insns.
  264.    The transitions (P->R and P->Q) are implemented in `schedule_insn' as
  265.    insns move from the ready list to the scheduled list.
  266.    The transition (Q->R) is implemented at the top of the scheduling
  267.    loop in `schedule_block' as time passes or stalls are introduced.  */
  268.  
  269. /* Implement a circular buffer to delay instructions until sufficient
  270.    time has passed.  INSN_QUEUE_SIZE is a power of two larger than
  271.    MAX_BLOCKAGE and MAX_READY_COST computed by genattr.c.  This is the
  272.    longest time an isnsn may be queued.  */
  273. static rtx insn_queue[INSN_QUEUE_SIZE];
  274. static int q_ptr = 0;
  275. static int q_size = 0;
  276. #define NEXT_Q(X) (((X)+1) & (INSN_QUEUE_SIZE-1))
  277. #define NEXT_Q_AFTER(X,C) (((X)+C) & (INSN_QUEUE_SIZE-1))
  278.  
  279. /* Vector indexed by INSN_UID giving the minimum clock tick at which
  280.    the insn becomes ready.  This is used to note timing constraints for
  281.    insns in the pending list.  */
  282. static int *insn_tick;
  283. #define INSN_TICK(INSN) (insn_tick[INSN_UID (INSN)])
  284.  
  285. /* Data structure for keeping track of register information
  286.    during that register's life.  */
  287.  
  288. struct sometimes
  289. {
  290.   short offset; short bit;
  291.   short live_length; short calls_crossed;
  292. };
  293.  
  294. /* Forward declarations.  */
  295. static rtx canon_rtx            PROTO((rtx));
  296. static int rtx_equal_for_memref_p    PROTO((rtx, rtx));
  297. static rtx find_symbolic_term        PROTO((rtx));
  298. static int memrefs_conflict_p        PROTO((int, rtx, int, rtx,
  299.                            HOST_WIDE_INT));
  300. static void add_dependence        PROTO((rtx, rtx, enum reg_note));
  301. static void remove_dependence        PROTO((rtx, rtx));
  302. static rtx find_insn_list        PROTO((rtx, rtx));
  303. static int insn_unit            PROTO((rtx));
  304. static unsigned int blockage_range    PROTO((int, rtx));
  305. static void clear_units            PROTO((void));
  306. static void prepare_unit        PROTO((int));
  307. static int actual_hazard_this_instance    PROTO((int, int, rtx, int, int));
  308. static void schedule_unit        PROTO((int, rtx, int));
  309. static int actual_hazard        PROTO((int, rtx, int, int));
  310. static int potential_hazard        PROTO((int, rtx, int));
  311. static int insn_cost            PROTO((rtx, rtx, rtx));
  312. static int priority            PROTO((rtx));
  313. static void free_pending_lists        PROTO((void));
  314. static void add_insn_mem_dependence    PROTO((rtx *, rtx *, rtx, rtx));
  315. static void flush_pending_lists        PROTO((rtx));
  316. static void sched_analyze_1        PROTO((rtx, rtx));
  317. static void sched_analyze_2        PROTO((rtx, rtx));
  318. static void sched_analyze_insn        PROTO((rtx, rtx));
  319. static int sched_analyze        PROTO((rtx, rtx));
  320. static void sched_note_set        PROTO((int, rtx, int));
  321. static int rank_for_schedule        PROTO((rtx *, rtx *));
  322. static void swap_sort            PROTO((rtx *, int));
  323. static void queue_insn            PROTO((rtx, int));
  324. static int birthing_insn        PROTO((rtx));
  325. static void adjust_priority        PROTO((rtx));
  326. static int schedule_insn        PROTO((rtx, rtx *, int, int));
  327. static int schedule_select        PROTO((rtx *, int, int, FILE *));
  328. static void create_reg_dead_note    PROTO((rtx, rtx));
  329. static void attach_deaths        PROTO((rtx, rtx, int));
  330. static void attach_deaths_insn        PROTO((rtx));
  331. static rtx unlink_notes            PROTO((rtx, rtx));
  332. static int new_sometimes_live        PROTO((struct sometimes *, int, int,
  333.                            int));
  334. static void finish_sometimes_live    PROTO((struct sometimes *, int));
  335. static void schedule_block        PROTO((int, FILE *));
  336. static rtx regno_use_in            PROTO((int, rtx));
  337. static void split_hard_reg_notes    PROTO((rtx, rtx, rtx, rtx));
  338. static void new_insn_dead_notes        PROTO((rtx, rtx, rtx, rtx));
  339. static void update_n_sets        PROTO((rtx, int));
  340. static void update_flow_info        PROTO((rtx, rtx, rtx, rtx));
  341.  
  342. /* Main entry point of this file.  */
  343. void schedule_insns    PROTO((FILE *));
  344.  
  345. #endif /* INSN_SCHEDULING */
  346.  
  347. #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
  348.  
  349. /* Vector indexed by N giving the initial (unchanging) value known
  350.    for pseudo-register N.  */
  351. static rtx *reg_known_value;
  352.  
  353. /* Vector recording for each reg_known_value whether it is due to a
  354.    REG_EQUIV note.  Future passes (viz., reload) may replace the
  355.    pseudo with the equivalent expression and so we account for the
  356.    dependences that would be introduced if that happens. */
  357. /* ??? This is a problem only on the Convex.  The REG_EQUIV notes created in
  358.    assign_parms mention the arg pointer, and there are explicit insns in the
  359.    RTL that modify the arg pointer.  Thus we must ensure that such insns don't
  360.    get scheduled across each other because that would invalidate the REG_EQUIV
  361.    notes.  One could argue that the REG_EQUIV notes are wrong, but solving
  362.    the problem in the scheduler will likely give better code, so we do it
  363.    here.  */
  364. static char *reg_known_equiv_p;
  365.  
  366. /* Indicates number of valid entries in reg_known_value.  */
  367. static int reg_known_value_size;
  368.  
  369. static rtx
  370. canon_rtx (x)
  371.      rtx x;
  372. {
  373.   if (GET_CODE (x) == REG && REGNO (x) >= FIRST_PSEUDO_REGISTER
  374.       && REGNO (x) <= reg_known_value_size)
  375.     return reg_known_value[REGNO (x)];
  376.   else if (GET_CODE (x) == PLUS)
  377.     {
  378.       rtx x0 = canon_rtx (XEXP (x, 0));
  379.       rtx x1 = canon_rtx (XEXP (x, 1));
  380.  
  381.       if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
  382.     {
  383.       /* We can tolerate LO_SUMs being offset here; these
  384.          rtl are used for nothing other than comparisons.  */
  385.       if (GET_CODE (x0) == CONST_INT)
  386.         return plus_constant_for_output (x1, INTVAL (x0));
  387.       else if (GET_CODE (x1) == CONST_INT)
  388.         return plus_constant_for_output (x0, INTVAL (x1));
  389.       return gen_rtx (PLUS, GET_MODE (x), x0, x1);
  390.     }
  391.     }
  392.   return x;
  393. }
  394.  
  395. /* Set up all info needed to perform alias analysis on memory references.  */
  396.  
  397. void
  398. init_alias_analysis ()
  399. {
  400.   int maxreg = max_reg_num ();
  401.   rtx insn;
  402.   rtx note;
  403.   rtx set;
  404.  
  405.   reg_known_value_size = maxreg;
  406.  
  407.   reg_known_value
  408.     = (rtx *) oballoc ((maxreg-FIRST_PSEUDO_REGISTER) * sizeof (rtx))
  409.       - FIRST_PSEUDO_REGISTER;
  410.   bzero ((char *) (reg_known_value + FIRST_PSEUDO_REGISTER),
  411.      (maxreg-FIRST_PSEUDO_REGISTER) * sizeof (rtx));
  412.  
  413.   reg_known_equiv_p
  414.     = (char *) oballoc ((maxreg -FIRST_PSEUDO_REGISTER) * sizeof (char))
  415.       - FIRST_PSEUDO_REGISTER;
  416.   bzero (reg_known_equiv_p + FIRST_PSEUDO_REGISTER,
  417.      (maxreg - FIRST_PSEUDO_REGISTER) * sizeof (char));
  418.  
  419.   /* Fill in the entries with known constant values.  */
  420.   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
  421.     if ((set = single_set (insn)) != 0
  422.     && GET_CODE (SET_DEST (set)) == REG
  423.     && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
  424.     && (((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
  425.          && reg_n_sets[REGNO (SET_DEST (set))] == 1)
  426.         || (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != 0)
  427.     && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
  428.       {
  429.     int regno = REGNO (SET_DEST (set));
  430.     reg_known_value[regno] = XEXP (note, 0);
  431.     reg_known_equiv_p[regno] = REG_NOTE_KIND (note) == REG_EQUIV;
  432.       }
  433.  
  434.   /* Fill in the remaining entries.  */
  435.   while (--maxreg >= FIRST_PSEUDO_REGISTER)
  436.     if (reg_known_value[maxreg] == 0)
  437.       reg_known_value[maxreg] = regno_reg_rtx[maxreg];
  438. }
  439.  
  440. /* Return 1 if X and Y are identical-looking rtx's.
  441.  
  442.    We use the data in reg_known_value above to see if two registers with
  443.    different numbers are, in fact, equivalent.  */
  444.  
  445. static int
  446. rtx_equal_for_memref_p (x, y)
  447.      rtx x, y;
  448. {
  449.   register int i;
  450.   register int j;
  451.   register enum rtx_code code;
  452.   register char *fmt;
  453.  
  454.   if (x == 0 && y == 0)
  455.     return 1;
  456.   if (x == 0 || y == 0)
  457.     return 0;
  458.   x = canon_rtx (x);
  459.   y = canon_rtx (y);
  460.  
  461.   if (x == y)
  462.     return 1;
  463.  
  464.   code = GET_CODE (x);
  465.   /* Rtx's of different codes cannot be equal.  */
  466.   if (code != GET_CODE (y))
  467.     return 0;
  468.  
  469.   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
  470.      (REG:SI x) and (REG:HI x) are NOT equivalent.  */
  471.  
  472.   if (GET_MODE (x) != GET_MODE (y))
  473.     return 0;
  474.  
  475.   /* REG, LABEL_REF, and SYMBOL_REF can be compared nonrecursively.  */
  476.  
  477.   if (code == REG)
  478.     return REGNO (x) == REGNO (y);
  479.   if (code == LABEL_REF)
  480.     return XEXP (x, 0) == XEXP (y, 0);
  481.   if (code == SYMBOL_REF)
  482.     return XSTR (x, 0) == XSTR (y, 0);
  483.  
  484.   /* For commutative operations, the RTX match if the operand match in any
  485.      order.  Also handle the simple binary and unary cases without a loop.  */
  486.   if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
  487.     return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
  488.          && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
  489.         || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
  490.         && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
  491.   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
  492.     return (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
  493.         && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)));
  494.   else if (GET_RTX_CLASS (code) == '1')
  495.     return rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0));
  496.  
  497.   /* Compare the elements.  If any pair of corresponding elements
  498.      fail to match, return 0 for the whole things.  */
  499.  
  500.   fmt = GET_RTX_FORMAT (code);
  501.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  502.     {
  503.       switch (fmt[i])
  504.     {
  505.     case 'w':
  506.       if (XWINT (x, i) != XWINT (y, i))
  507.         return 0;
  508.       break;
  509.  
  510.     case 'n':
  511.     case 'i':
  512.       if (XINT (x, i) != XINT (y, i))
  513.         return 0;
  514.       break;
  515.  
  516.     case 'V':
  517.     case 'E':
  518.       /* Two vectors must have the same length.  */
  519.       if (XVECLEN (x, i) != XVECLEN (y, i))
  520.         return 0;
  521.  
  522.       /* And the corresponding elements must match.  */
  523.       for (j = 0; j < XVECLEN (x, i); j++)
  524.         if (rtx_equal_for_memref_p (XVECEXP (x, i, j), XVECEXP (y, i, j)) == 0)
  525.           return 0;
  526.       break;
  527.  
  528.     case 'e':
  529.       if (rtx_equal_for_memref_p (XEXP (x, i), XEXP (y, i)) == 0)
  530.         return 0;
  531.       break;
  532.  
  533.     case 'S':
  534.     case 's':
  535.       if (strcmp (XSTR (x, i), XSTR (y, i)))
  536.         return 0;
  537.       break;
  538.  
  539.     case 'u':
  540.       /* These are just backpointers, so they don't matter.  */
  541.       break;
  542.  
  543.     case '0':
  544.       break;
  545.  
  546.       /* It is believed that rtx's at this level will never
  547.          contain anything but integers and other rtx's,
  548.          except for within LABEL_REFs and SYMBOL_REFs.  */
  549.     default:
  550.       abort ();
  551.     }
  552.     }
  553.   return 1;
  554. }
  555.  
  556. /* Given an rtx X, find a SYMBOL_REF or LABEL_REF within
  557.    X and return it, or return 0 if none found.  */
  558.  
  559. static rtx
  560. find_symbolic_term (x)
  561.      rtx x;
  562. {
  563.   register int i;
  564.   register enum rtx_code code;
  565.   register char *fmt;
  566.  
  567.   code = GET_CODE (x);
  568.   if (code == SYMBOL_REF || code == LABEL_REF)
  569.     return x;
  570.   if (GET_RTX_CLASS (code) == 'o')
  571.     return 0;
  572.  
  573.   fmt = GET_RTX_FORMAT (code);
  574.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  575.     {
  576.       rtx t;
  577.  
  578.       if (fmt[i] == 'e')
  579.     {
  580.       t = find_symbolic_term (XEXP (x, i));
  581.       if (t != 0)
  582.         return t;
  583.     }
  584.       else if (fmt[i] == 'E')
  585.     break;
  586.     }
  587.   return 0;
  588. }
  589.  
  590. /* Return nonzero if X and Y (memory addresses) could reference the
  591.    same location in memory.  C is an offset accumulator.  When
  592.    C is nonzero, we are testing aliases between X and Y + C.
  593.    XSIZE is the size in bytes of the X reference,
  594.    similarly YSIZE is the size in bytes for Y.
  595.  
  596.    If XSIZE or YSIZE is zero, we do not know the amount of memory being
  597.    referenced (the reference was BLKmode), so make the most pessimistic
  598.    assumptions.
  599.  
  600.    We recognize the following cases of non-conflicting memory:
  601.  
  602.     (1) addresses involving the frame pointer cannot conflict
  603.         with addresses involving static variables.
  604.     (2) static variables with different addresses cannot conflict.
  605.  
  606.    Nice to notice that varying addresses cannot conflict with fp if no
  607.    local variables had their addresses taken, but that's too hard now.  */
  608.  
  609. /* ??? In Fortran, references to a array parameter can never conflict with
  610.    another array parameter.  */
  611.  
  612. static int
  613. memrefs_conflict_p (xsize, x, ysize, y, c)
  614.      rtx x, y;
  615.      int xsize, ysize;
  616.      HOST_WIDE_INT c;
  617. {
  618.   if (GET_CODE (x) == HIGH)
  619.     x = XEXP (x, 0);
  620.   else if (GET_CODE (x) == LO_SUM)
  621.     x = XEXP (x, 1);
  622.   else
  623.     x = canon_rtx (x);
  624.   if (GET_CODE (y) == HIGH)
  625.     y = XEXP (y, 0);
  626.   else if (GET_CODE (y) == LO_SUM)
  627.     y = XEXP (y, 1);
  628.   else
  629.     y = canon_rtx (y);
  630.  
  631.   if (rtx_equal_for_memref_p (x, y))
  632.     return (xsize == 0 || ysize == 0 ||
  633.         (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
  634.  
  635.   if (y == frame_pointer_rtx || y == hard_frame_pointer_rtx
  636.       || y == stack_pointer_rtx)
  637.     {
  638.       rtx t = y;
  639.       int tsize = ysize;
  640.       y = x; ysize = xsize;
  641.       x = t; xsize = tsize;
  642.     }
  643.  
  644.   if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
  645.       || x == stack_pointer_rtx)
  646.     {
  647.       rtx y1;
  648.  
  649.       if (CONSTANT_P (y))
  650.     return 0;
  651.  
  652.       if (GET_CODE (y) == PLUS
  653.       && canon_rtx (XEXP (y, 0)) == x
  654.       && (y1 = canon_rtx (XEXP (y, 1)))
  655.       && GET_CODE (y1) == CONST_INT)
  656.     {
  657.       c += INTVAL (y1);
  658.       return (xsize == 0 || ysize == 0
  659.           || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
  660.     }
  661.  
  662.       if (GET_CODE (y) == PLUS
  663.       && (y1 = canon_rtx (XEXP (y, 0)))
  664.       && CONSTANT_P (y1))
  665.     return 0;
  666.  
  667.       return 1;
  668.     }
  669.  
  670.   if (GET_CODE (x) == PLUS)
  671.     {
  672.       /* The fact that X is canonicalized means that this
  673.      PLUS rtx is canonicalized.  */
  674.       rtx x0 = XEXP (x, 0);
  675.       rtx x1 = XEXP (x, 1);
  676.  
  677.       if (GET_CODE (y) == PLUS)
  678.     {
  679.       /* The fact that Y is canonicalized means that this
  680.          PLUS rtx is canonicalized.  */
  681.       rtx y0 = XEXP (y, 0);
  682.       rtx y1 = XEXP (y, 1);
  683.  
  684.       if (rtx_equal_for_memref_p (x1, y1))
  685.         return memrefs_conflict_p (xsize, x0, ysize, y0, c);
  686.       if (rtx_equal_for_memref_p (x0, y0))
  687.         return memrefs_conflict_p (xsize, x1, ysize, y1, c);
  688.       if (GET_CODE (x1) == CONST_INT)
  689.         if (GET_CODE (y1) == CONST_INT)
  690.           return memrefs_conflict_p (xsize, x0, ysize, y0,
  691.                      c - INTVAL (x1) + INTVAL (y1));
  692.         else
  693.           return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
  694.       else if (GET_CODE (y1) == CONST_INT)
  695.         return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
  696.  
  697.       /* Handle case where we cannot understand iteration operators,
  698.          but we notice that the base addresses are distinct objects.  */
  699.       x = find_symbolic_term (x);
  700.       if (x == 0)
  701.         return 1;
  702.       y = find_symbolic_term (y);
  703.       if (y == 0)
  704.         return 1;
  705.       return rtx_equal_for_memref_p (x, y);
  706.     }
  707.       else if (GET_CODE (x1) == CONST_INT)
  708.     return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
  709.     }
  710.   else if (GET_CODE (y) == PLUS)
  711.     {
  712.       /* The fact that Y is canonicalized means that this
  713.      PLUS rtx is canonicalized.  */
  714.       rtx y0 = XEXP (y, 0);
  715.       rtx y1 = XEXP (y, 1);
  716.  
  717.       if (GET_CODE (y1) == CONST_INT)
  718.     return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
  719.       else
  720.     return 1;
  721.     }
  722.  
  723.   if (GET_CODE (x) == GET_CODE (y))
  724.     switch (GET_CODE (x))
  725.       {
  726.       case MULT:
  727.     {
  728.       /* Handle cases where we expect the second operands to be the
  729.          same, and check only whether the first operand would conflict
  730.          or not.  */
  731.       rtx x0, y0;
  732.       rtx x1 = canon_rtx (XEXP (x, 1));
  733.       rtx y1 = canon_rtx (XEXP (y, 1));
  734.       if (! rtx_equal_for_memref_p (x1, y1))
  735.         return 1;
  736.       x0 = canon_rtx (XEXP (x, 0));
  737.       y0 = canon_rtx (XEXP (y, 0));
  738.       if (rtx_equal_for_memref_p (x0, y0))
  739.         return (xsize == 0 || ysize == 0
  740.             || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
  741.  
  742.       /* Can't properly adjust our sizes.  */
  743.       if (GET_CODE (x1) != CONST_INT)
  744.         return 1;
  745.       xsize /= INTVAL (x1);
  746.       ysize /= INTVAL (x1);
  747.       c /= INTVAL (x1);
  748.       return memrefs_conflict_p (xsize, x0, ysize, y0, c);
  749.     }
  750.       }
  751.  
  752.   if (CONSTANT_P (x))
  753.     {
  754.       if (GET_CODE (x) == CONST_INT && GET_CODE (y) == CONST_INT)
  755.     {
  756.       c += (INTVAL (y) - INTVAL (x));
  757.       return (xsize == 0 || ysize == 0
  758.           || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
  759.     }
  760.  
  761.       if (GET_CODE (x) == CONST)
  762.     {
  763.       if (GET_CODE (y) == CONST)
  764.         return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
  765.                        ysize, canon_rtx (XEXP (y, 0)), c);
  766.       else
  767.         return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
  768.                        ysize, y, c);
  769.     }
  770.       if (GET_CODE (y) == CONST)
  771.     return memrefs_conflict_p (xsize, x, ysize,
  772.                    canon_rtx (XEXP (y, 0)), c);
  773.  
  774.       if (CONSTANT_P (y))
  775.     return (rtx_equal_for_memref_p (x, y)
  776.         && (xsize == 0 || ysize == 0
  777.             || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0)));
  778.  
  779.       return 1;
  780.     }
  781.   return 1;
  782. }
  783.  
  784. /* Functions to compute memory dependencies.
  785.  
  786.    Since we process the insns in execution order, we can build tables
  787.    to keep track of what registers are fixed (and not aliased), what registers
  788.    are varying in known ways, and what registers are varying in unknown
  789.    ways.
  790.  
  791.    If both memory references are volatile, then there must always be a
  792.    dependence between the two references, since their order can not be
  793.    changed.  A volatile and non-volatile reference can be interchanged
  794.    though. 
  795.  
  796.    A MEM_IN_STRUCT reference at a non-QImode varying address can never
  797.    conflict with a non-MEM_IN_STRUCT reference at a fixed address.   We must
  798.    allow QImode aliasing because the ANSI C standard allows character
  799.    pointers to alias anything.  We are assuming that characters are
  800.    always QImode here.  */
  801.  
  802. /* Read dependence: X is read after read in MEM takes place.  There can
  803.    only be a dependence here if both reads are volatile.  */
  804.  
  805. int
  806. read_dependence (mem, x)
  807.      rtx mem;
  808.      rtx x;
  809. {
  810.   return MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem);
  811. }
  812.  
  813. /* True dependence: X is read after store in MEM takes place.  */
  814.  
  815. int
  816. true_dependence (mem, x)
  817.      rtx mem;
  818.      rtx x;
  819. {
  820.   /* If X is an unchanging read, then it can't possibly conflict with any
  821.      non-unchanging store.  It may conflict with an unchanging write though,
  822.      because there may be a single store to this address to initialize it.
  823.      Just fall through to the code below to resolve the case where we have
  824.      both an unchanging read and an unchanging write.  This won't handle all
  825.      cases optimally, but the possible performance loss should be
  826.      negligible.  */
  827.   if (RTX_UNCHANGING_P (x) && ! RTX_UNCHANGING_P (mem))
  828.     return 0;
  829.  
  830.   return ((MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
  831.       || (memrefs_conflict_p (SIZE_FOR_MODE (mem), XEXP (mem, 0),
  832.                   SIZE_FOR_MODE (x), XEXP (x, 0), 0)
  833.           && ! (MEM_IN_STRUCT_P (mem) && rtx_addr_varies_p (mem)
  834.             && GET_MODE (mem) != QImode
  835.             && ! MEM_IN_STRUCT_P (x) && ! rtx_addr_varies_p (x))
  836.           && ! (MEM_IN_STRUCT_P (x) && rtx_addr_varies_p (x)
  837.             && GET_MODE (x) != QImode
  838.             && ! MEM_IN_STRUCT_P (mem) && ! rtx_addr_varies_p (mem))));
  839. }
  840.  
  841. /* Anti dependence: X is written after read in MEM takes place.  */
  842.  
  843. int
  844. anti_dependence (mem, x)
  845.      rtx mem;
  846.      rtx x;
  847. {
  848.   /* If MEM is an unchanging read, then it can't possibly conflict with
  849.      the store to X, because there is at most one store to MEM, and it must
  850.      have occured somewhere before MEM.  */
  851.   if (RTX_UNCHANGING_P (mem))
  852.     return 0;
  853.  
  854.   return ((MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
  855.       || (memrefs_conflict_p (SIZE_FOR_MODE (mem), XEXP (mem, 0),
  856.                   SIZE_FOR_MODE (x), XEXP (x, 0), 0)
  857.           && ! (MEM_IN_STRUCT_P (mem) && rtx_addr_varies_p (mem)
  858.             && GET_MODE (mem) != QImode
  859.             && ! MEM_IN_STRUCT_P (x) && ! rtx_addr_varies_p (x))
  860.           && ! (MEM_IN_STRUCT_P (x) && rtx_addr_varies_p (x)
  861.             && GET_MODE (x) != QImode
  862.             && ! MEM_IN_STRUCT_P (mem) && ! rtx_addr_varies_p (mem))));
  863. }
  864.  
  865. /* Output dependence: X is written after store in MEM takes place.  */
  866.  
  867. int
  868. output_dependence (mem, x)
  869.      rtx mem;
  870.      rtx x;
  871. {
  872.   return ((MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
  873.       || (memrefs_conflict_p (SIZE_FOR_MODE (mem), XEXP (mem, 0),
  874.                   SIZE_FOR_MODE (x), XEXP (x, 0), 0)
  875.           && ! (MEM_IN_STRUCT_P (mem) && rtx_addr_varies_p (mem)
  876.             && GET_MODE (mem) != QImode
  877.             && ! MEM_IN_STRUCT_P (x) && ! rtx_addr_varies_p (x))
  878.           && ! (MEM_IN_STRUCT_P (x) && rtx_addr_varies_p (x)
  879.             && GET_MODE (x) != QImode
  880.             && ! MEM_IN_STRUCT_P (mem) && ! rtx_addr_varies_p (mem))));
  881. }
  882.  
  883. /* Helper functions for instruction scheduling.  */
  884.  
  885. /* Add ELEM wrapped in an INSN_LIST with reg note kind DEP_TYPE to the
  886.    LOG_LINKS of INSN, if not already there.  DEP_TYPE indicates the type
  887.    of dependence that this link represents.  */
  888.  
  889. static void
  890. add_dependence (insn, elem, dep_type)
  891.      rtx insn;
  892.      rtx elem;
  893.      enum reg_note dep_type;
  894. {
  895.   rtx link, next;
  896.  
  897.   /* Don't depend an insn on itself.  */
  898.   if (insn == elem)
  899.     return;
  900.  
  901.   /* If elem is part of a sequence that must be scheduled together, then
  902.      make the dependence point to the last insn of the sequence.
  903.      When HAVE_cc0, it is possible for NOTEs to exist between users and
  904.      setters of the condition codes, so we must skip past notes here.
  905.      Otherwise, NOTEs are impossible here.  */
  906.  
  907.   next = NEXT_INSN (elem);
  908.  
  909. #ifdef HAVE_cc0
  910.   while (next && GET_CODE (next) == NOTE)
  911.     next = NEXT_INSN (next);
  912. #endif
  913.  
  914.   if (next && SCHED_GROUP_P (next))
  915.     {
  916.       /* Notes will never intervene here though, so don't bother checking
  917.      for them.  */
  918.       /* We must reject CODE_LABELs, so that we don't get confused by one
  919.      that has LABEL_PRESERVE_P set, which is represented by the same
  920.      bit in the rtl as SCHED_GROUP_P.  A CODE_LABEL can never be
  921.      SCHED_GROUP_P.  */
  922.       while (NEXT_INSN (next) && SCHED_GROUP_P (NEXT_INSN (next))
  923.          && GET_CODE (NEXT_INSN (next)) != CODE_LABEL)
  924.     next = NEXT_INSN (next);
  925.  
  926.       /* Again, don't depend an insn on itself.  */
  927.       if (insn == next)
  928.     return;
  929.  
  930.       /* Make the dependence to NEXT, the last insn of the group, instead
  931.      of the original ELEM.  */
  932.       elem = next;
  933.     }
  934.  
  935.   /* Check that we don't already have this dependence.  */
  936.   for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
  937.     if (XEXP (link, 0) == elem)
  938.       {
  939.     /* If this is a more restrictive type of dependence than the existing
  940.        one, then change the existing dependence to this type.  */
  941.     if ((int) dep_type < (int) REG_NOTE_KIND (link))
  942.       PUT_REG_NOTE_KIND (link, dep_type);
  943.     return;
  944.       }
  945.   /* Might want to check one level of transitivity to save conses.  */
  946.  
  947.   link = rtx_alloc (INSN_LIST);
  948.   /* Insn dependency, not data dependency.  */
  949.   PUT_REG_NOTE_KIND (link, dep_type);
  950.   XEXP (link, 0) = elem;
  951.   XEXP (link, 1) = LOG_LINKS (insn);
  952.   LOG_LINKS (insn) = link;
  953. }
  954.  
  955. /* Remove ELEM wrapped in an INSN_LIST from the LOG_LINKS
  956.    of INSN.  Abort if not found.  */
  957.  
  958. static void
  959. remove_dependence (insn, elem)
  960.      rtx insn;
  961.      rtx elem;
  962. {
  963.   rtx prev, link;
  964.   int found = 0;
  965.  
  966.   for (prev = 0, link = LOG_LINKS (insn); link;
  967.        prev = link, link = XEXP (link, 1))
  968.     {
  969.       if (XEXP (link, 0) == elem)
  970.     {
  971.       if (prev)
  972.         XEXP (prev, 1) = XEXP (link, 1);
  973.       else
  974.         LOG_LINKS (insn) = XEXP (link, 1);
  975.       found = 1;
  976.     }
  977.     }
  978.  
  979.   if (! found)
  980.     abort ();
  981.   return;
  982. }
  983.  
  984. #ifndef INSN_SCHEDULING
  985. void
  986. schedule_insns (dump_file)
  987.      FILE *dump_file;
  988. {
  989. }
  990. #else
  991. #ifndef __GNUC__
  992. #define __inline
  993. #endif
  994.  
  995. /* Computation of memory dependencies.  */
  996.  
  997. /* The *_insns and *_mems are paired lists.  Each pending memory operation
  998.    will have a pointer to the MEM rtx on one list and a pointer to the
  999.    containing insn on the other list in the same place in the list.  */
  1000.  
  1001. /* We can't use add_dependence like the old code did, because a single insn
  1002.    may have multiple memory accesses, and hence needs to be on the list
  1003.    once for each memory access.  Add_dependence won't let you add an insn
  1004.    to a list more than once.  */
  1005.  
  1006. /* An INSN_LIST containing all insns with pending read operations.  */
  1007. static rtx pending_read_insns;
  1008.  
  1009. /* An EXPR_LIST containing all MEM rtx's which are pending reads.  */
  1010. static rtx pending_read_mems;
  1011.  
  1012. /* An INSN_LIST containing all insns with pending write operations.  */
  1013. static rtx pending_write_insns;
  1014.  
  1015. /* An EXPR_LIST containing all MEM rtx's which are pending writes.  */
  1016. static rtx pending_write_mems;
  1017.  
  1018. /* Indicates the combined length of the two pending lists.  We must prevent
  1019.    these lists from ever growing too large since the number of dependencies
  1020.    produced is at least O(N*N), and execution time is at least O(4*N*N), as
  1021.    a function of the length of these pending lists.  */
  1022.  
  1023. static int pending_lists_length;
  1024.  
  1025. /* An INSN_LIST containing all INSN_LISTs allocated but currently unused.  */
  1026.  
  1027. static rtx unused_insn_list;
  1028.  
  1029. /* An EXPR_LIST containing all EXPR_LISTs allocated but currently unused.  */
  1030.  
  1031. static rtx unused_expr_list;
  1032.  
  1033. /* The last insn upon which all memory references must depend.
  1034.    This is an insn which flushed the pending lists, creating a dependency
  1035.    between it and all previously pending memory references.  This creates
  1036.    a barrier (or a checkpoint) which no memory reference is allowed to cross.
  1037.  
  1038.    This includes all non constant CALL_INSNs.  When we do interprocedural
  1039.    alias analysis, this restriction can be relaxed.
  1040.    This may also be an INSN that writes memory if the pending lists grow
  1041.    too large.  */
  1042.  
  1043. static rtx last_pending_memory_flush;
  1044.  
  1045. /* The last function call we have seen.  All hard regs, and, of course,
  1046.    the last function call, must depend on this.  */
  1047.  
  1048. static rtx last_function_call;
  1049.  
  1050. /* The LOG_LINKS field of this is a list of insns which use a pseudo register
  1051.    that does not already cross a call.  We create dependencies between each
  1052.    of those insn and the next call insn, to ensure that they won't cross a call
  1053.    after scheduling is done.  */
  1054.  
  1055. static rtx sched_before_next_call;
  1056.  
  1057. /* Pointer to the last instruction scheduled.  Used by rank_for_schedule,
  1058.    so that insns independent of the last scheduled insn will be preferred
  1059.    over dependent instructions.  */
  1060.  
  1061. static rtx last_scheduled_insn;
  1062.  
  1063. /* Process an insn's memory dependencies.  There are four kinds of
  1064.    dependencies:
  1065.  
  1066.    (0) read dependence: read follows read
  1067.    (1) true dependence: read follows write
  1068.    (2) anti dependence: write follows read
  1069.    (3) output dependence: write follows write
  1070.  
  1071.    We are careful to build only dependencies which actually exist, and
  1072.    use transitivity to avoid building too many links.  */
  1073.  
  1074. /* Return the INSN_LIST containing INSN in LIST, or NULL
  1075.    if LIST does not contain INSN.  */
  1076.  
  1077. __inline static rtx
  1078. find_insn_list (insn, list)
  1079.      rtx insn;
  1080.      rtx list;
  1081. {
  1082.   while (list)
  1083.     {
  1084.       if (XEXP (list, 0) == insn)
  1085.     return list;
  1086.       list = XEXP (list, 1);
  1087.     }
  1088.   return 0;
  1089. }
  1090.  
  1091. /* Compute the function units used by INSN.  This caches the value
  1092.    returned by function_units_used.  A function unit is encoded as the
  1093.    unit number if the value is non-negative and the compliment of a
  1094.    mask if the value is negative.  A function unit index is the
  1095.    non-negative encoding.  */
  1096.  
  1097. __inline static int
  1098. insn_unit (insn)
  1099.      rtx insn;
  1100. {
  1101.   register int unit = INSN_UNIT (insn);
  1102.  
  1103.   if (unit == 0)
  1104.     {
  1105.       recog_memoized (insn);
  1106.  
  1107.       /* A USE insn, or something else we don't need to understand.
  1108.      We can't pass these directly to function_units_used because it will
  1109.      trigger a fatal error for unrecognizable insns.  */
  1110.       if (INSN_CODE (insn) < 0)
  1111.     unit = -1;
  1112.       else
  1113.     {
  1114.       unit = function_units_used (insn);
  1115.       /* Increment non-negative values so we can cache zero.  */
  1116.       if (unit >= 0) unit++;
  1117.     }
  1118.       /* We only cache 16 bits of the result, so if the value is out of
  1119.      range, don't cache it.  */
  1120.       if (FUNCTION_UNITS_SIZE < HOST_BITS_PER_SHORT
  1121.       || unit >= 0
  1122.       || (~unit & ((1 << (HOST_BITS_PER_SHORT - 1)) - 1)) == 0)
  1123.       INSN_UNIT (insn) = unit;
  1124.     }
  1125.   return (unit > 0 ? unit - 1 : unit);
  1126. }
  1127.  
  1128. /* Compute the blockage range for executing INSN on UNIT.  This caches
  1129.    the value returned by the blockage_range_function for the unit.
  1130.    These values are encoded in an int where the upper half gives the
  1131.    minimum value and the lower half gives the maximum value.  */
  1132.  
  1133. __inline static unsigned int
  1134. blockage_range (unit, insn)
  1135.      int unit;
  1136.      rtx insn;
  1137. {
  1138.   unsigned int blockage = INSN_BLOCKAGE (insn);
  1139.   unsigned int range;
  1140.  
  1141.   if (UNIT_BLOCKED (blockage) != unit + 1)
  1142.     {
  1143.       range = function_units[unit].blockage_range_function (insn);
  1144.       /* We only cache the blockage range for one unit and then only if
  1145.      the values fit.  */
  1146.       if (HOST_BITS_PER_INT >= UNIT_BITS + 2 * BLOCKAGE_BITS)
  1147.     INSN_BLOCKAGE (insn) = ENCODE_BLOCKAGE (unit + 1, range);
  1148.     }
  1149.   else
  1150.     range = BLOCKAGE_RANGE (blockage);
  1151.  
  1152.   return range;
  1153. }
  1154.  
  1155. /* A vector indexed by function unit instance giving the last insn to use
  1156.    the unit.  The value of the function unit instance index for unit U
  1157.    instance I is (U + I * FUNCTION_UNITS_SIZE).  */
  1158. static rtx unit_last_insn[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
  1159.  
  1160. /* A vector indexed by function unit instance giving the minimum time when
  1161.    the unit will unblock based on the maximum blockage cost.  */
  1162. static int unit_tick[FUNCTION_UNITS_SIZE * MAX_MULTIPLICITY];
  1163.  
  1164. /* A vector indexed by function unit number giving the number of insns
  1165.    that remain to use the unit.  */
  1166. static int unit_n_insns[FUNCTION_UNITS_SIZE];
  1167.  
  1168. /* Reset the function unit state to the null state.  */
  1169.  
  1170. static void
  1171. clear_units ()
  1172. {
  1173.   bzero ((char *) unit_last_insn, sizeof (unit_last_insn));
  1174.   bzero ((char *) unit_tick, sizeof (unit_tick));
  1175.   bzero ((char *) unit_n_insns, sizeof (unit_n_insns));
  1176. }
  1177.  
  1178. /* Record an insn as one that will use the units encoded by UNIT.  */
  1179.  
  1180. __inline static void
  1181. prepare_unit (unit)
  1182.      int unit;
  1183. {
  1184.   int i;
  1185.  
  1186.   if (unit >= 0)
  1187.     unit_n_insns[unit]++;
  1188.   else
  1189.     for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
  1190.       if ((unit & 1) != 0)
  1191.     prepare_unit (i);
  1192. }
  1193.  
  1194. /* Return the actual hazard cost of executing INSN on the unit UNIT,
  1195.    instance INSTANCE at time CLOCK if the previous actual hazard cost
  1196.    was COST.  */
  1197.  
  1198. __inline static int
  1199. actual_hazard_this_instance (unit, instance, insn, clock, cost)
  1200.      int unit, instance, clock, cost;
  1201.      rtx insn;
  1202. {
  1203.   int tick = unit_tick[instance];
  1204.  
  1205.   if (tick - clock > cost)
  1206.     {
  1207.       /* The scheduler is operating in reverse, so INSN is the executing
  1208.      insn and the unit's last insn is the candidate insn.  We want a
  1209.      more exact measure of the blockage if we execute INSN at CLOCK
  1210.      given when we committed the execution of the unit's last insn.
  1211.  
  1212.      The blockage value is given by either the unit's max blockage
  1213.      constant, blockage range function, or blockage function.  Use
  1214.      the most exact form for the given unit.  */
  1215.  
  1216.       if (function_units[unit].blockage_range_function)
  1217.     {
  1218.       if (function_units[unit].blockage_function)
  1219.         tick += (function_units[unit].blockage_function
  1220.              (insn, unit_last_insn[instance])
  1221.              - function_units[unit].max_blockage);
  1222.       else
  1223.         tick += ((int) MAX_BLOCKAGE_COST (blockage_range (unit, insn))
  1224.              - function_units[unit].max_blockage);
  1225.     }
  1226.       if (tick - clock > cost)
  1227.     cost = tick - clock;
  1228.     }
  1229.   return cost;
  1230. }
  1231.  
  1232. /* Record INSN as having begun execution on the units encoded by UNIT at
  1233.    time CLOCK.  */
  1234.  
  1235. __inline static void
  1236. schedule_unit (unit, insn, clock)
  1237.      int unit, clock;
  1238.      rtx insn;
  1239. {
  1240.   int i;
  1241.  
  1242.   if (unit >= 0)
  1243.     {
  1244.       int instance = unit;
  1245. #if MAX_MULTIPLICITY > 1
  1246.       /* Find the first free instance of the function unit and use that
  1247.      one.  We assume that one is free.  */
  1248.       for (i = function_units[unit].multiplicity - 1; i > 0; i--)
  1249.     {
  1250.       if (! actual_hazard_this_instance (unit, instance, insn, clock, 0))
  1251.         break;
  1252.       instance += FUNCTION_UNITS_SIZE;
  1253.     }
  1254. #endif
  1255.       unit_last_insn[instance] = insn;
  1256.       unit_tick[instance] = (clock + function_units[unit].max_blockage);
  1257.     }
  1258.   else
  1259.     for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
  1260.       if ((unit & 1) != 0)
  1261.     schedule_unit (i, insn, clock);
  1262. }
  1263.  
  1264. /* Return the actual hazard cost of executing INSN on the units encoded by
  1265.    UNIT at time CLOCK if the previous actual hazard cost was COST.  */
  1266.  
  1267. __inline static int
  1268. actual_hazard (unit, insn, clock, cost)
  1269.      int unit, clock, cost;
  1270.      rtx insn;
  1271. {
  1272.   int i;
  1273.  
  1274.   if (unit >= 0)
  1275.     {
  1276.       /* Find the instance of the function unit with the minimum hazard.  */
  1277.       int instance = unit;
  1278.       int best_cost = actual_hazard_this_instance (unit, instance, insn,
  1279.                            clock, cost);
  1280.       int this_cost;
  1281.  
  1282. #if MAX_MULTIPLICITY > 1
  1283.       if (best_cost > cost)
  1284.     {
  1285.       for (i = function_units[unit].multiplicity - 1; i > 0; i--)
  1286.         {
  1287.           instance += FUNCTION_UNITS_SIZE;
  1288.           this_cost = actual_hazard_this_instance (unit, instance, insn,
  1289.                                clock, cost);
  1290.           if (this_cost < best_cost)
  1291.         {
  1292.           best_cost = this_cost;
  1293.           if (this_cost <= cost)
  1294.             break;
  1295.         }
  1296.         }
  1297.     }
  1298. #endif
  1299.       cost = MAX (cost, best_cost);
  1300.     }
  1301.   else
  1302.     for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
  1303.       if ((unit & 1) != 0)
  1304.     cost = actual_hazard (i, insn, clock, cost);
  1305.  
  1306.   return cost;
  1307. }
  1308.  
  1309. /* Return the potential hazard cost of executing an instruction on the
  1310.    units encoded by UNIT if the previous potential hazard cost was COST.
  1311.    An insn with a large blockage time is chosen in preference to one
  1312.    with a smaller time; an insn that uses a unit that is more likely
  1313.    to be used is chosen in preference to one with a unit that is less
  1314.    used.  We are trying to minimize a subsequent actual hazard.  */
  1315.  
  1316. __inline static int
  1317. potential_hazard (unit, insn, cost)
  1318.      int unit, cost;
  1319.      rtx insn;
  1320. {
  1321.   int i, ncost;
  1322.   unsigned int minb, maxb;
  1323.  
  1324.   if (unit >= 0)
  1325.     {
  1326.       minb = maxb = function_units[unit].max_blockage;
  1327.       if (maxb > 1)
  1328.     {
  1329.       if (function_units[unit].blockage_range_function)
  1330.         {
  1331.           maxb = minb = blockage_range (unit, insn);
  1332.           maxb = MAX_BLOCKAGE_COST (maxb);
  1333.           minb = MIN_BLOCKAGE_COST (minb);
  1334.         }
  1335.  
  1336.       if (maxb > 1)
  1337.         {
  1338.           /* Make the number of instructions left dominate.  Make the
  1339.          minimum delay dominate the maximum delay.  If all these
  1340.          are the same, use the unit number to add an arbitrary
  1341.          ordering.  Other terms can be added.  */
  1342.           ncost = minb * 0x40 + maxb;
  1343.           ncost *= (unit_n_insns[unit] - 1) * 0x1000 + unit;
  1344.           if (ncost > cost)
  1345.         cost = ncost;
  1346.         }
  1347.     }
  1348.     }
  1349.   else
  1350.     for (i = 0, unit = ~unit; unit; i++, unit >>= 1)
  1351.       if ((unit & 1) != 0)
  1352.     cost = potential_hazard (i, insn, cost);
  1353.  
  1354.   return cost;
  1355. }
  1356.  
  1357. /* Compute cost of executing INSN given the dependence LINK on the insn USED.
  1358.    This is the number of virtual cycles taken between instruction issue and
  1359.    instruction results.  */
  1360.  
  1361. __inline static int
  1362. insn_cost (insn, link, used)
  1363.      rtx insn, link, used;
  1364. {
  1365.   register int cost = INSN_COST (insn);
  1366.  
  1367.   if (cost == 0)
  1368.     {
  1369.       recog_memoized (insn);
  1370.  
  1371.       /* A USE insn, or something else we don't need to understand.
  1372.      We can't pass these directly to result_ready_cost because it will
  1373.      trigger a fatal error for unrecognizable insns.  */
  1374.       if (INSN_CODE (insn) < 0)
  1375.     {
  1376.       INSN_COST (insn) = 1;
  1377.       return 1;
  1378.     }
  1379.       else
  1380.     {
  1381.       cost = result_ready_cost (insn);
  1382.  
  1383.       if (cost < 1)
  1384.         cost = 1;
  1385.  
  1386.       INSN_COST (insn) = cost;
  1387.     }
  1388.     }
  1389.  
  1390.   /* A USE insn should never require the value used to be computed.  This
  1391.      allows the computation of a function's result and parameter values to
  1392.      overlap the return and call.  */
  1393.   recog_memoized (used);
  1394.   if (INSN_CODE (used) < 0)
  1395.     LINK_COST_FREE (link) = 1;
  1396.  
  1397.   /* If some dependencies vary the cost, compute the adjustment.  Most
  1398.      commonly, the adjustment is complete: either the cost is ignored
  1399.      (in the case of an output- or anti-dependence), or the cost is
  1400.      unchanged.  These values are cached in the link as LINK_COST_FREE
  1401.      and LINK_COST_ZERO.  */
  1402.  
  1403.   if (LINK_COST_FREE (link))
  1404.     cost = 1;
  1405. #ifdef ADJUST_COST
  1406.   else if (! LINK_COST_ZERO (link))
  1407.     {
  1408.       int ncost = cost;
  1409.  
  1410.       ADJUST_COST (used, link, insn, ncost);
  1411.       if (ncost <= 1)
  1412.     LINK_COST_FREE (link) = ncost = 1;
  1413.       if (cost == ncost)
  1414.     LINK_COST_ZERO (link) = 1;
  1415.       cost = ncost;
  1416.     }
  1417. #endif
  1418.   return cost;
  1419. }
  1420.  
  1421. /* Compute the priority number for INSN.  */
  1422.  
  1423. static int
  1424. priority (insn)
  1425.      rtx insn;
  1426. {
  1427.   if (insn && GET_RTX_CLASS (GET_CODE (insn)) == 'i')
  1428.     {
  1429.       int prev_priority;
  1430.       int max_priority;
  1431.       int this_priority = INSN_PRIORITY (insn);
  1432.       rtx prev;
  1433.  
  1434.       if (this_priority > 0)
  1435.     return this_priority;
  1436.  
  1437.       max_priority = 1;
  1438.  
  1439.       /* Nonzero if these insns must be scheduled together.  */
  1440.       if (SCHED_GROUP_P (insn))
  1441.     {
  1442.       prev = insn;
  1443.       while (SCHED_GROUP_P (prev))
  1444.         {
  1445.           prev = PREV_INSN (prev);
  1446.           INSN_REF_COUNT (prev) += 1;
  1447.         }
  1448.     }
  1449.  
  1450.       for (prev = LOG_LINKS (insn); prev; prev = XEXP (prev, 1))
  1451.     {
  1452.       rtx x = XEXP (prev, 0);
  1453.  
  1454.       /* A dependence pointing to a note or deleted insn is always
  1455.          obsolete, because sched_analyze_insn will have created any
  1456.          necessary new dependences which replace it.  Notes and deleted
  1457.          insns can be created when instructions are deleted by insn
  1458.          splitting, or by register allocation.  */
  1459.       if (GET_CODE (x) == NOTE || INSN_DELETED_P (x))
  1460.         {
  1461.           remove_dependence (insn, x);
  1462.           continue;
  1463.         }
  1464.  
  1465.       /* Clear the link cost adjustment bits.  */
  1466.       LINK_COST_FREE (prev) = 0;
  1467. #ifdef ADJUST_COST
  1468.       LINK_COST_ZERO (prev) = 0;
  1469. #endif
  1470.  
  1471.       /* This priority calculation was chosen because it results in the
  1472.          least instruction movement, and does not hurt the performance
  1473.          of the resulting code compared to the old algorithm.
  1474.          This makes the sched algorithm more stable, which results
  1475.          in better code, because there is less register pressure,
  1476.          cross jumping is more likely to work, and debugging is easier.
  1477.  
  1478.          When all instructions have a latency of 1, there is no need to
  1479.          move any instructions.  Subtracting one here ensures that in such
  1480.          cases all instructions will end up with a priority of one, and
  1481.          hence no scheduling will be done.
  1482.  
  1483.          The original code did not subtract the one, and added the
  1484.          insn_cost of the current instruction to its priority (e.g.
  1485.          move the insn_cost call down to the end).  */
  1486.  
  1487.       prev_priority = priority (x) + insn_cost (x, prev, insn) - 1;
  1488.  
  1489.       if (prev_priority > max_priority)
  1490.         max_priority = prev_priority;
  1491.       INSN_REF_COUNT (x) += 1;
  1492.     }
  1493.  
  1494.       prepare_unit (insn_unit (insn));
  1495.       INSN_PRIORITY (insn) = max_priority;
  1496.       return INSN_PRIORITY (insn);
  1497.     }
  1498.   return 0;
  1499. }
  1500.  
  1501. /* Remove all INSN_LISTs and EXPR_LISTs from the pending lists and add
  1502.    them to the unused_*_list variables, so that they can be reused.  */
  1503.  
  1504. static void
  1505. free_pending_lists ()
  1506. {
  1507.   register rtx link, prev_link;
  1508.  
  1509.   if (pending_read_insns)
  1510.     {
  1511.       prev_link = pending_read_insns;
  1512.       link = XEXP (prev_link, 1);
  1513.  
  1514.       while (link)
  1515.     {
  1516.       prev_link = link;
  1517.       link = XEXP (link, 1);
  1518.     }
  1519.  
  1520.       XEXP (prev_link, 1) = unused_insn_list;
  1521.       unused_insn_list = pending_read_insns;
  1522.       pending_read_insns = 0;
  1523.     }
  1524.  
  1525.   if (pending_write_insns)
  1526.     {
  1527.       prev_link = pending_write_insns;
  1528.       link = XEXP (prev_link, 1);
  1529.  
  1530.       while (link)
  1531.     {
  1532.       prev_link = link;
  1533.       link = XEXP (link, 1);
  1534.     }
  1535.  
  1536.       XEXP (prev_link, 1) = unused_insn_list;
  1537.       unused_insn_list = pending_write_insns;
  1538.       pending_write_insns = 0;
  1539.     }
  1540.  
  1541.   if (pending_read_mems)
  1542.     {
  1543.       prev_link = pending_read_mems;
  1544.       link = XEXP (prev_link, 1);
  1545.  
  1546.       while (link)
  1547.     {
  1548.       prev_link = link;
  1549.       link = XEXP (link, 1);
  1550.     }
  1551.  
  1552.       XEXP (prev_link, 1) = unused_expr_list;
  1553.       unused_expr_list = pending_read_mems;
  1554.       pending_read_mems = 0;
  1555.     }
  1556.  
  1557.   if (pending_write_mems)
  1558.     {
  1559.       prev_link = pending_write_mems;
  1560.       link = XEXP (prev_link, 1);
  1561.  
  1562.       while (link)
  1563.     {
  1564.       prev_link = link;
  1565.       link = XEXP (link, 1);
  1566.     }
  1567.  
  1568.       XEXP (prev_link, 1) = unused_expr_list;
  1569.       unused_expr_list = pending_write_mems;
  1570.       pending_write_mems = 0;
  1571.     }
  1572. }
  1573.  
  1574. /* Add an INSN and MEM reference pair to a pending INSN_LIST and MEM_LIST.
  1575.    The MEM is a memory reference contained within INSN, which we are saving
  1576.    so that we can do memory aliasing on it.  */
  1577.  
  1578. static void
  1579. add_insn_mem_dependence (insn_list, mem_list, insn, mem)
  1580.      rtx *insn_list, *mem_list, insn, mem;
  1581. {
  1582.   register rtx link;
  1583.  
  1584.   if (unused_insn_list)
  1585.     {
  1586.       link = unused_insn_list;
  1587.       unused_insn_list = XEXP (link, 1);
  1588.     }
  1589.   else
  1590.     link = rtx_alloc (INSN_LIST);
  1591.   XEXP (link, 0) = insn;
  1592.   XEXP (link, 1) = *insn_list;
  1593.   *insn_list = link;
  1594.  
  1595.   if (unused_expr_list)
  1596.     {
  1597.       link = unused_expr_list;
  1598.       unused_expr_list = XEXP (link, 1);
  1599.     }
  1600.   else
  1601.     link = rtx_alloc (EXPR_LIST);
  1602.   XEXP (link, 0) = mem;
  1603.   XEXP (link, 1) = *mem_list;
  1604.   *mem_list = link;
  1605.  
  1606.   pending_lists_length++;
  1607. }
  1608.  
  1609. /* Make a dependency between every memory reference on the pending lists
  1610.    and INSN, thus flushing the pending lists.  */
  1611.  
  1612. static void
  1613. flush_pending_lists (insn)
  1614.      rtx insn;
  1615. {
  1616.   rtx link;
  1617.  
  1618.   while (pending_read_insns)
  1619.     {
  1620.       add_dependence (insn, XEXP (pending_read_insns, 0), REG_DEP_ANTI);
  1621.  
  1622.       link = pending_read_insns;
  1623.       pending_read_insns = XEXP (pending_read_insns, 1);
  1624.       XEXP (link, 1) = unused_insn_list;
  1625.       unused_insn_list = link;
  1626.  
  1627.       link = pending_read_mems;
  1628.       pending_read_mems = XEXP (pending_read_mems, 1);
  1629.       XEXP (link, 1) = unused_expr_list;
  1630.       unused_expr_list = link;
  1631.     }
  1632.   while (pending_write_insns)
  1633.     {
  1634.       add_dependence (insn, XEXP (pending_write_insns, 0), REG_DEP_ANTI);
  1635.  
  1636.       link = pending_write_insns;
  1637.       pending_write_insns = XEXP (pending_write_insns, 1);
  1638.       XEXP (link, 1) = unused_insn_list;
  1639.       unused_insn_list = link;
  1640.  
  1641.       link = pending_write_mems;
  1642.       pending_write_mems = XEXP (pending_write_mems, 1);
  1643.       XEXP (link, 1) = unused_expr_list;
  1644.       unused_expr_list = link;
  1645.     }
  1646.   pending_lists_length = 0;
  1647.  
  1648.   if (last_pending_memory_flush)
  1649.     add_dependence (insn, last_pending_memory_flush, REG_DEP_ANTI);
  1650.  
  1651.   last_pending_memory_flush = insn;
  1652. }
  1653.  
  1654. /* Analyze a single SET or CLOBBER rtx, X, creating all dependencies generated
  1655.    by the write to the destination of X, and reads of everything mentioned.  */
  1656.  
  1657. static void
  1658. sched_analyze_1 (x, insn)
  1659.      rtx x;
  1660.      rtx insn;
  1661. {
  1662.   register int regno;
  1663.   register rtx dest = SET_DEST (x);
  1664.  
  1665.   if (dest == 0)
  1666.     return;
  1667.  
  1668.   while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
  1669.      || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
  1670.     {
  1671.       if (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
  1672.     {
  1673.       /* The second and third arguments are values read by this insn.  */
  1674.       sched_analyze_2 (XEXP (dest, 1), insn);
  1675.       sched_analyze_2 (XEXP (dest, 2), insn);
  1676.     }
  1677.       dest = SUBREG_REG (dest);
  1678.     }
  1679.  
  1680.   if (GET_CODE (dest) == REG)
  1681.     {
  1682.       register int i;
  1683.  
  1684.       regno = REGNO (dest);
  1685.  
  1686.       /* A hard reg in a wide mode may really be multiple registers.
  1687.      If so, mark all of them just like the first.  */
  1688.       if (regno < FIRST_PSEUDO_REGISTER)
  1689.     {
  1690.       i = HARD_REGNO_NREGS (regno, GET_MODE (dest));
  1691.       while (--i >= 0)
  1692.         {
  1693.           rtx u;
  1694.  
  1695.           for (u = reg_last_uses[regno+i]; u; u = XEXP (u, 1))
  1696.         add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
  1697.           reg_last_uses[regno + i] = 0;
  1698.           if (reg_last_sets[regno + i])
  1699.         add_dependence (insn, reg_last_sets[regno + i],
  1700.                 REG_DEP_OUTPUT);
  1701.           reg_pending_sets[(regno + i) / REGSET_ELT_BITS]
  1702.         |= (REGSET_ELT_TYPE) 1 << ((regno + i) % REGSET_ELT_BITS);
  1703.           if ((call_used_regs[i] || global_regs[i])
  1704.           && last_function_call)
  1705.         /* Function calls clobber all call_used regs.  */
  1706.         add_dependence (insn, last_function_call, REG_DEP_ANTI);
  1707.         }
  1708.     }
  1709.       else
  1710.     {
  1711.       rtx u;
  1712.  
  1713.       for (u = reg_last_uses[regno]; u; u = XEXP (u, 1))
  1714.         add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
  1715.       reg_last_uses[regno] = 0;
  1716.       if (reg_last_sets[regno])
  1717.         add_dependence (insn, reg_last_sets[regno], REG_DEP_OUTPUT);
  1718.       reg_pending_sets[regno / REGSET_ELT_BITS]
  1719.         |= (REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS);
  1720.  
  1721.       /* Pseudos that are REG_EQUIV to something may be replaced
  1722.          by that during reloading.  We need only add dependencies for
  1723.          the address in the REG_EQUIV note.  */
  1724.       if (! reload_completed
  1725.           && reg_known_equiv_p[regno]
  1726.           && GET_CODE (reg_known_value[regno]) == MEM)
  1727.         sched_analyze_2 (XEXP (reg_known_value[regno], 0), insn);
  1728.  
  1729.       /* Don't let it cross a call after scheduling if it doesn't
  1730.          already cross one.  */
  1731.       if (reg_n_calls_crossed[regno] == 0 && last_function_call)
  1732.         add_dependence (insn, last_function_call, REG_DEP_ANTI);
  1733.     }
  1734.     }
  1735.   else if (GET_CODE (dest) == MEM)
  1736.     {
  1737.       /* Writing memory.  */
  1738.  
  1739.       if (pending_lists_length > 32)
  1740.     {
  1741.       /* Flush all pending reads and writes to prevent the pending lists
  1742.          from getting any larger.  Insn scheduling runs too slowly when
  1743.          these lists get long.  The number 32 was chosen because it
  1744.          seems like a reasonable number.  When compiling GCC with itself,
  1745.          this flush occurs 8 times for sparc, and 10 times for m88k using
  1746.          the number 32.  */
  1747.       flush_pending_lists (insn);
  1748.     }
  1749.       else
  1750.     {
  1751.       rtx pending, pending_mem;
  1752.  
  1753.       pending = pending_read_insns;
  1754.       pending_mem = pending_read_mems;
  1755.       while (pending)
  1756.         {
  1757.           /* If a dependency already exists, don't create a new one.  */
  1758.           if (! find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
  1759.         if (anti_dependence (XEXP (pending_mem, 0), dest))
  1760.           add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
  1761.  
  1762.           pending = XEXP (pending, 1);
  1763.           pending_mem = XEXP (pending_mem, 1);
  1764.         }
  1765.  
  1766.       pending = pending_write_insns;
  1767.       pending_mem = pending_write_mems;
  1768.       while (pending)
  1769.         {
  1770.           /* If a dependency already exists, don't create a new one.  */
  1771.           if (! find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
  1772.         if (output_dependence (XEXP (pending_mem, 0), dest))
  1773.           add_dependence (insn, XEXP (pending, 0), REG_DEP_OUTPUT);
  1774.  
  1775.           pending = XEXP (pending, 1);
  1776.           pending_mem = XEXP (pending_mem, 1);
  1777.         }
  1778.  
  1779.       if (last_pending_memory_flush)
  1780.         add_dependence (insn, last_pending_memory_flush, REG_DEP_ANTI);
  1781.  
  1782.       add_insn_mem_dependence (&pending_write_insns, &pending_write_mems,
  1783.                    insn, dest);
  1784.     }
  1785.       sched_analyze_2 (XEXP (dest, 0), insn);
  1786.     }
  1787.  
  1788.   /* Analyze reads.  */
  1789.   if (GET_CODE (x) == SET)
  1790.     sched_analyze_2 (SET_SRC (x), insn);
  1791. }
  1792.  
  1793. /* Analyze the uses of memory and registers in rtx X in INSN.  */
  1794.  
  1795. static void
  1796. sched_analyze_2 (x, insn)
  1797.      rtx x;
  1798.      rtx insn;
  1799. {
  1800.   register int i;
  1801.   register int j;
  1802.   register enum rtx_code code;
  1803.   register char *fmt;
  1804.  
  1805.   if (x == 0)
  1806.     return;
  1807.  
  1808.   code = GET_CODE (x);
  1809.  
  1810.   switch (code)
  1811.     {
  1812.     case CONST_INT:
  1813.     case CONST_DOUBLE:
  1814.     case SYMBOL_REF:
  1815.     case CONST:
  1816.     case LABEL_REF:
  1817.       /* Ignore constants.  Note that we must handle CONST_DOUBLE here
  1818.      because it may have a cc0_rtx in its CONST_DOUBLE_CHAIN field, but
  1819.      this does not mean that this insn is using cc0.  */
  1820.       return;
  1821.  
  1822. #ifdef HAVE_cc0
  1823.     case CC0:
  1824.       {
  1825.     rtx link, prev;
  1826.  
  1827.     /* There may be a note before this insn now, but all notes will
  1828.        be removed before we actually try to schedule the insns, so
  1829.        it won't cause a problem later.  We must avoid it here though.  */
  1830.  
  1831.     /* User of CC0 depends on immediately preceding insn.  */
  1832.     SCHED_GROUP_P (insn) = 1;
  1833.  
  1834.     /* Make a copy of all dependencies on the immediately previous insn,
  1835.        and add to this insn.  This is so that all the dependencies will
  1836.        apply to the group.  Remove an explicit dependence on this insn
  1837.        as SCHED_GROUP_P now represents it.  */
  1838.  
  1839.     prev = PREV_INSN (insn);
  1840.     while (GET_CODE (prev) == NOTE)
  1841.       prev = PREV_INSN (prev);
  1842.  
  1843.     if (find_insn_list (prev, LOG_LINKS (insn)))
  1844.       remove_dependence (insn, prev);
  1845.  
  1846.     for (link = LOG_LINKS (prev); link; link = XEXP (link, 1))
  1847.       add_dependence (insn, XEXP (link, 0), REG_NOTE_KIND (link));
  1848.  
  1849.     return;
  1850.       }
  1851. #endif
  1852.  
  1853.     case REG:
  1854.       {
  1855.     int regno = REGNO (x);
  1856.     if (regno < FIRST_PSEUDO_REGISTER)
  1857.       {
  1858.         int i;
  1859.  
  1860.         i = HARD_REGNO_NREGS (regno, GET_MODE (x));
  1861.         while (--i >= 0)
  1862.           {
  1863.         reg_last_uses[regno + i]
  1864.           = gen_rtx (INSN_LIST, VOIDmode,
  1865.                  insn, reg_last_uses[regno + i]);
  1866.         if (reg_last_sets[regno + i])
  1867.           add_dependence (insn, reg_last_sets[regno + i], 0);
  1868.         if ((call_used_regs[regno + i] || global_regs[regno + i])
  1869.             && last_function_call)
  1870.           /* Function calls clobber all call_used regs.  */
  1871.           add_dependence (insn, last_function_call, REG_DEP_ANTI);
  1872.           }
  1873.       }
  1874.     else
  1875.       {
  1876.         reg_last_uses[regno]
  1877.           = gen_rtx (INSN_LIST, VOIDmode, insn, reg_last_uses[regno]);
  1878.         if (reg_last_sets[regno])
  1879.           add_dependence (insn, reg_last_sets[regno], 0);
  1880.  
  1881.         /* Pseudos that are REG_EQUIV to something may be replaced
  1882.            by that during reloading.  We need only add dependencies for
  1883.            the address in the REG_EQUIV note.  */
  1884.         if (! reload_completed
  1885.         && reg_known_equiv_p[regno]
  1886.         && GET_CODE (reg_known_value[regno]) == MEM)
  1887.           sched_analyze_2 (XEXP (reg_known_value[regno], 0), insn);
  1888.  
  1889.         /* If the register does not already cross any calls, then add this
  1890.            insn to the sched_before_next_call list so that it will still
  1891.            not cross calls after scheduling.  */
  1892.         if (reg_n_calls_crossed[regno] == 0)
  1893.           add_dependence (sched_before_next_call, insn, REG_DEP_ANTI);
  1894.       }
  1895.     return;
  1896.       }
  1897.  
  1898.     case MEM:
  1899.       {
  1900.     /* Reading memory.  */
  1901.  
  1902.     rtx pending, pending_mem;
  1903.  
  1904.     pending = pending_read_insns;
  1905.     pending_mem = pending_read_mems;
  1906.     while (pending)
  1907.       {
  1908.         /* If a dependency already exists, don't create a new one.  */
  1909.         if (! find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
  1910.           if (read_dependence (XEXP (pending_mem, 0), x))
  1911.         add_dependence (insn, XEXP (pending, 0), REG_DEP_ANTI);
  1912.  
  1913.         pending = XEXP (pending, 1);
  1914.         pending_mem = XEXP (pending_mem, 1);
  1915.       }
  1916.  
  1917.     pending = pending_write_insns;
  1918.     pending_mem = pending_write_mems;
  1919.     while (pending)
  1920.       {
  1921.         /* If a dependency already exists, don't create a new one.  */
  1922.         if (! find_insn_list (XEXP (pending, 0), LOG_LINKS (insn)))
  1923.           if (true_dependence (XEXP (pending_mem, 0), x))
  1924.         add_dependence (insn, XEXP (pending, 0), 0);
  1925.  
  1926.         pending = XEXP (pending, 1);
  1927.         pending_mem = XEXP (pending_mem, 1);
  1928.       }
  1929.     if (last_pending_memory_flush)
  1930.       add_dependence (insn, last_pending_memory_flush, REG_DEP_ANTI);
  1931.  
  1932.     /* Always add these dependencies to pending_reads, since
  1933.        this insn may be followed by a write.  */
  1934.     add_insn_mem_dependence (&pending_read_insns, &pending_read_mems,
  1935.                  insn, x);
  1936.  
  1937.     /* Take advantage of tail recursion here.  */
  1938.     sched_analyze_2 (XEXP (x, 0), insn);
  1939.     return;
  1940.       }
  1941.  
  1942.     case ASM_OPERANDS:
  1943.     case ASM_INPUT:
  1944.     case UNSPEC_VOLATILE:
  1945.     case TRAP_IF:
  1946.       {
  1947.     rtx u;
  1948.  
  1949.     /* Traditional and volatile asm instructions must be considered to use
  1950.        and clobber all hard registers, all pseudo-registers and all of
  1951.        memory.  So must TRAP_IF and UNSPEC_VOLATILE operations.
  1952.  
  1953.        Consider for instance a volatile asm that changes the fpu rounding
  1954.        mode.  An insn should not be moved across this even if it only uses
  1955.        pseudo-regs because it might give an incorrectly rounded result.  */
  1956.     if (code != ASM_OPERANDS || MEM_VOLATILE_P (x))
  1957.       {
  1958.         int max_reg = max_reg_num ();
  1959.         for (i = 0; i < max_reg; i++)
  1960.           {
  1961.         for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
  1962.           add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
  1963.         reg_last_uses[i] = 0;
  1964.         if (reg_last_sets[i])
  1965.           add_dependence (insn, reg_last_sets[i], 0);
  1966.           }
  1967.         reg_pending_sets_all = 1;
  1968.  
  1969.         flush_pending_lists (insn);
  1970.       }
  1971.  
  1972.     /* For all ASM_OPERANDS, we must traverse the vector of input operands.
  1973.        We can not just fall through here since then we would be confused
  1974.        by the ASM_INPUT rtx inside ASM_OPERANDS, which do not indicate
  1975.        traditional asms unlike their normal usage.  */
  1976.  
  1977.     if (code == ASM_OPERANDS)
  1978.       {
  1979.         for (j = 0; j < ASM_OPERANDS_INPUT_LENGTH (x); j++)
  1980.           sched_analyze_2 (ASM_OPERANDS_INPUT (x, j), insn);
  1981.         return;
  1982.       }
  1983.     break;
  1984.       }
  1985.  
  1986.     case PRE_DEC:
  1987.     case POST_DEC:
  1988.     case PRE_INC:
  1989.     case POST_INC:
  1990.       /* These both read and modify the result.  We must handle them as writes
  1991.      to get proper dependencies for following instructions.  We must handle
  1992.      them as reads to get proper dependencies from this to previous
  1993.      instructions.  Thus we need to pass them to both sched_analyze_1
  1994.      and sched_analyze_2.  We must call sched_analyze_2 first in order
  1995.      to get the proper antecedent for the read.  */
  1996.       sched_analyze_2 (XEXP (x, 0), insn);
  1997.       sched_analyze_1 (x, insn);
  1998.       return;
  1999.     }
  2000.  
  2001.   /* Other cases: walk the insn.  */
  2002.   fmt = GET_RTX_FORMAT (code);
  2003.   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  2004.     {
  2005.       if (fmt[i] == 'e')
  2006.     sched_analyze_2 (XEXP (x, i), insn);
  2007.       else if (fmt[i] == 'E')
  2008.     for (j = 0; j < XVECLEN (x, i); j++)
  2009.       sched_analyze_2 (XVECEXP (x, i, j), insn);
  2010.     }
  2011. }
  2012.  
  2013. /* Analyze an INSN with pattern X to find all dependencies.  */
  2014.  
  2015. static void
  2016. sched_analyze_insn (x, insn)
  2017.      rtx x, insn;
  2018. {
  2019.   register RTX_CODE code = GET_CODE (x);
  2020.   rtx link;
  2021.   int maxreg = max_reg_num ();
  2022.   int i;
  2023.  
  2024.   if (code == SET || code == CLOBBER)
  2025.     sched_analyze_1 (x, insn);
  2026.   else if (code == PARALLEL)
  2027.     {
  2028.       register int i;
  2029.       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
  2030.     {
  2031.       code = GET_CODE (XVECEXP (x, 0, i));
  2032.       if (code == SET || code == CLOBBER)
  2033.         sched_analyze_1 (XVECEXP (x, 0, i), insn);
  2034.       else
  2035.         sched_analyze_2 (XVECEXP (x, 0, i), insn);
  2036.     }
  2037.     }
  2038.   else
  2039.     sched_analyze_2 (x, insn);
  2040.  
  2041.   /* Mark registers CLOBBERED or used by called function.  */
  2042.   if (GET_CODE (insn) == CALL_INSN)
  2043.     for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
  2044.       {
  2045.     if (GET_CODE (XEXP (link, 0)) == CLOBBER)
  2046.       sched_analyze_1 (XEXP (link, 0), insn);
  2047.     else
  2048.       sched_analyze_2 (XEXP (link, 0), insn);
  2049.       }
  2050.  
  2051.   /* After reload, it is possible for an instruction to have a REG_DEAD note
  2052.      for a register that actually dies a few instructions earlier.  For
  2053.      example, this can happen with SECONDARY_MEMORY_NEEDED reloads.
  2054.      In this case, we must consider the insn to use the register mentioned
  2055.      in the REG_DEAD note.  Otherwise, we may accidentally move this insn
  2056.      after another insn that sets the register, thus getting obviously invalid
  2057.      rtl.  This confuses reorg which believes that REG_DEAD notes are still
  2058.      meaningful.
  2059.  
  2060.      ??? We would get better code if we fixed reload to put the REG_DEAD
  2061.      notes in the right places, but that may not be worth the effort.  */
  2062.  
  2063.   if (reload_completed)
  2064.     {
  2065.       rtx note;
  2066.  
  2067.       for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
  2068.     if (REG_NOTE_KIND (note) == REG_DEAD)
  2069.       sched_analyze_2 (XEXP (note, 0), insn);
  2070.     }
  2071.  
  2072.   for (i = 0; i < regset_size; i++)
  2073.     {
  2074.       REGSET_ELT_TYPE sets = reg_pending_sets[i];
  2075.       if (sets)
  2076.     {
  2077.       register int bit;
  2078.       for (bit = 0; bit < REGSET_ELT_BITS; bit++)
  2079.         if (sets & ((REGSET_ELT_TYPE) 1 << bit))
  2080.           reg_last_sets[i * REGSET_ELT_BITS + bit] = insn;
  2081.       reg_pending_sets[i] = 0;
  2082.     }
  2083.     }
  2084.   if (reg_pending_sets_all)
  2085.     {
  2086.       for (i = 0; i < maxreg; i++)
  2087.     reg_last_sets[i] = insn;
  2088.       reg_pending_sets_all = 0;
  2089.     }
  2090.  
  2091.   /* Handle function calls and function returns created by the epilogue
  2092.      threading code.  */
  2093.   if (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN)
  2094.     {
  2095.       rtx dep_insn;
  2096.       rtx prev_dep_insn;
  2097.  
  2098.       /* When scheduling instructions, we make sure calls don't lose their
  2099.      accompanying USE insns by depending them one on another in order.
  2100.  
  2101.      Also, we must do the same thing for returns created by the epilogue
  2102.      threading code.  Note this code works only in this special case,
  2103.      because other passes make no guarantee that they will never emit
  2104.      an instruction between a USE and a RETURN.  There is such a guarantee
  2105.      for USE instructions immediately before a call.  */
  2106.  
  2107.       prev_dep_insn = insn;
  2108.       dep_insn = PREV_INSN (insn);
  2109.       while (GET_CODE (dep_insn) == INSN
  2110.          && GET_CODE (PATTERN (dep_insn)) == USE)
  2111.     {
  2112.       SCHED_GROUP_P (prev_dep_insn) = 1;
  2113.  
  2114.       /* Make a copy of all dependencies on dep_insn, and add to insn.
  2115.          This is so that all of the dependencies will apply to the
  2116.          group.  */
  2117.  
  2118.       for (link = LOG_LINKS (dep_insn); link; link = XEXP (link, 1))
  2119.         add_dependence (insn, XEXP (link, 0), REG_NOTE_KIND (link));
  2120.  
  2121.       prev_dep_insn = dep_insn;
  2122.       dep_insn = PREV_INSN (dep_insn);
  2123.     }
  2124.     }
  2125. }
  2126.  
  2127. /* Analyze every insn between HEAD and TAIL inclusive, creating LOG_LINKS
  2128.    for every dependency.  */
  2129.  
  2130. static int
  2131. sched_analyze (head, tail)
  2132.      rtx head, tail;
  2133. {
  2134.   register rtx insn;
  2135.   register int n_insns = 0;
  2136.   register rtx u;
  2137.   register int luid = 0;
  2138.  
  2139.   for (insn = head; ; insn = NEXT_INSN (insn))
  2140.     {
  2141.       INSN_LUID (insn) = luid++;
  2142.  
  2143.       if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN)
  2144.     {
  2145.       sched_analyze_insn (PATTERN (insn), insn);
  2146.       n_insns += 1;
  2147.     }
  2148.       else if (GET_CODE (insn) == CALL_INSN)
  2149.     {
  2150.       rtx x;
  2151.       register int i;
  2152.  
  2153.       /* Any instruction using a hard register which may get clobbered
  2154.          by a call needs to be marked as dependent on this call.
  2155.          This prevents a use of a hard return reg from being moved
  2156.          past a void call (i.e. it does not explicitly set the hard
  2157.          return reg).  */
  2158.  
  2159.       /* If this call is followed by a NOTE_INSN_SETJMP, then assume that
  2160.          all registers, not just hard registers, may be clobbered by this
  2161.          call.  */
  2162.  
  2163.       /* Insn, being a CALL_INSN, magically depends on
  2164.          `last_function_call' already.  */
  2165.  
  2166.       if (NEXT_INSN (insn) && GET_CODE (NEXT_INSN (insn)) == NOTE
  2167.           && NOTE_LINE_NUMBER (NEXT_INSN (insn)) == NOTE_INSN_SETJMP)
  2168.         {
  2169.           int max_reg = max_reg_num ();
  2170.           for (i = 0; i < max_reg; i++)
  2171.         {
  2172.           for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
  2173.             add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
  2174.           reg_last_uses[i] = 0;
  2175.           if (reg_last_sets[i])
  2176.             add_dependence (insn, reg_last_sets[i], 0);
  2177.         }
  2178.           reg_pending_sets_all = 1;
  2179.  
  2180.           /* Add a fake REG_NOTE which we will later convert
  2181.          back into a NOTE_INSN_SETJMP note.  */
  2182.           REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_DEAD, constm1_rtx,
  2183.                       REG_NOTES (insn));
  2184.         }
  2185.       else
  2186.         {
  2187.           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  2188.         if (call_used_regs[i] || global_regs[i])
  2189.           {
  2190.             for (u = reg_last_uses[i]; u; u = XEXP (u, 1))
  2191.               add_dependence (insn, XEXP (u, 0), REG_DEP_ANTI);
  2192.             reg_last_uses[i] = 0;
  2193.             if (reg_last_sets[i])
  2194.               add_dependence (insn, reg_last_sets[i], REG_DEP_ANTI);
  2195.             reg_pending_sets[i / REGSET_ELT_BITS]
  2196.               |= (REGSET_ELT_TYPE) 1 << (i % REGSET_ELT_BITS);
  2197.           }
  2198.         }
  2199.  
  2200.       /* For each insn which shouldn't cross a call, add a dependence
  2201.          between that insn and this call insn.  */
  2202.       x = LOG_LINKS (sched_before_next_call);
  2203.       while (x)
  2204.         {
  2205.           add_dependence (insn, XEXP (x, 0), REG_DEP_ANTI);
  2206.           x = XEXP (x, 1);
  2207.         }
  2208.       LOG_LINKS (sched_before_next_call) = 0;
  2209.  
  2210.       sched_analyze_insn (PATTERN (insn), insn);
  2211.  
  2212.       /* We don't need to flush memory for a function call which does
  2213.          not involve memory.  */
  2214.       if (! CONST_CALL_P (insn))
  2215.         {
  2216.           /* In the absence of interprocedural alias analysis,
  2217.          we must flush all pending reads and writes, and
  2218.          start new dependencies starting from here.  */
  2219.           flush_pending_lists (insn);
  2220.         }
  2221.  
  2222.       /* Depend this function call (actually, the user of this
  2223.          function call) on all hard register clobberage.  */
  2224.       last_function_call = insn;
  2225.       n_insns += 1;
  2226.     }
  2227.  
  2228.       if (insn == tail)
  2229.     return n_insns;
  2230.     }
  2231. }
  2232.  
  2233. /* Called when we see a set of a register.  If death is true, then we are
  2234.    scanning backwards.  Mark that register as unborn.  If nobody says
  2235.    otherwise, that is how things will remain.  If death is false, then we
  2236.    are scanning forwards.  Mark that register as being born.  */
  2237.  
  2238. static void
  2239. sched_note_set (b, x, death)
  2240.      int b;
  2241.      rtx x;
  2242.      int death;
  2243. {
  2244.   register int regno;
  2245.   register rtx reg = SET_DEST (x);
  2246.   int subreg_p = 0;
  2247.  
  2248.   if (reg == 0)
  2249.     return;
  2250.  
  2251.   while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == STRICT_LOW_PART
  2252.      || GET_CODE (reg) == SIGN_EXTRACT || GET_CODE (reg) == ZERO_EXTRACT)
  2253.     {
  2254.       /* Must treat modification of just one hardware register of a multi-reg
  2255.      value or just a byte field of a register exactly the same way that
  2256.      mark_set_1 in flow.c does, i.e. anything except a paradoxical subreg
  2257.      does not kill the entire register.  */
  2258.       if (GET_CODE (reg) != SUBREG
  2259.       || REG_SIZE (SUBREG_REG (reg)) > REG_SIZE (reg))
  2260.     subreg_p = 1;
  2261.  
  2262.       reg = SUBREG_REG (reg);
  2263.     }
  2264.  
  2265.   if (GET_CODE (reg) != REG)
  2266.     return;
  2267.  
  2268.   /* Global registers are always live, so the code below does not apply
  2269.      to them.  */
  2270.  
  2271.   regno = REGNO (reg);
  2272.   if (regno >= FIRST_PSEUDO_REGISTER || ! global_regs[regno])
  2273.     {
  2274.       register int offset = regno / REGSET_ELT_BITS;
  2275.       register REGSET_ELT_TYPE bit
  2276.     = (REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS);
  2277.  
  2278.       if (death)
  2279.     {
  2280.       /* If we only set part of the register, then this set does not
  2281.          kill it.  */
  2282.       if (subreg_p)
  2283.         return;
  2284.  
  2285.       /* Try killing this register.  */
  2286.       if (regno < FIRST_PSEUDO_REGISTER)
  2287.         {
  2288.           int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
  2289.           while (--j >= 0)
  2290.         {
  2291.           offset = (regno + j) / REGSET_ELT_BITS;
  2292.           bit = (REGSET_ELT_TYPE) 1 << ((regno + j) % REGSET_ELT_BITS);
  2293.           
  2294.           bb_live_regs[offset] &= ~bit;
  2295.           bb_dead_regs[offset] |= bit;
  2296.         }
  2297.         }
  2298.       else
  2299.         {
  2300.           bb_live_regs[offset] &= ~bit;
  2301.           bb_dead_regs[offset] |= bit;
  2302.         }
  2303.     }
  2304.       else
  2305.     {
  2306.       /* Make the register live again.  */
  2307.       if (regno < FIRST_PSEUDO_REGISTER)
  2308.         {
  2309.           int j = HARD_REGNO_NREGS (regno, GET_MODE (reg));
  2310.           while (--j >= 0)
  2311.         {
  2312.           offset = (regno + j) / REGSET_ELT_BITS;
  2313.           bit = (REGSET_ELT_TYPE) 1 << ((regno + j) % REGSET_ELT_BITS);
  2314.           
  2315.           bb_live_regs[offset] |= bit;
  2316.           bb_dead_regs[offset] &= ~bit;
  2317.         }
  2318.         }
  2319.       else
  2320.         {
  2321.           bb_live_regs[offset] |= bit;
  2322.           bb_dead_regs[offset] &= ~bit;
  2323.         }
  2324.     }
  2325.     }
  2326. }
  2327.  
  2328. /* Macros and functions for keeping the priority queue sorted, and
  2329.    dealing with queueing and unqueueing of instructions.  */
  2330.  
  2331. #define SCHED_SORT(READY, NEW_READY, OLD_READY) \
  2332.   do { if ((NEW_READY) - (OLD_READY) == 1)                \
  2333.      swap_sort (READY, NEW_READY);                    \
  2334.        else if ((NEW_READY) - (OLD_READY) > 1)                \
  2335.      qsort (READY, NEW_READY, sizeof (rtx), rank_for_schedule); }    \
  2336.   while (0)
  2337.  
  2338. /* Returns a positive value if y is preferred; returns a negative value if
  2339.    x is preferred.  Should never return 0, since that will make the sort
  2340.    unstable.  */
  2341.  
  2342. static int
  2343. rank_for_schedule (x, y)
  2344.      rtx *x, *y;
  2345. {
  2346.   rtx tmp = *y;
  2347.   rtx tmp2 = *x;
  2348.   rtx link;
  2349.   int tmp_class, tmp2_class;
  2350.   int value;
  2351.  
  2352.   /* Choose the instruction with the highest priority, if different.  */
  2353.   if (value = INSN_PRIORITY (tmp) - INSN_PRIORITY (tmp2))
  2354.     return value;
  2355.  
  2356.   if (last_scheduled_insn)
  2357.     {
  2358.       /* Classify the instructions into three classes:
  2359.      1) Data dependent on last schedule insn.
  2360.      2) Anti/Output dependent on last scheduled insn.
  2361.      3) Independent of last scheduled insn, or has latency of one.
  2362.      Choose the insn from the highest numbered class if different.  */
  2363.       link = find_insn_list (tmp, LOG_LINKS (last_scheduled_insn));
  2364.       if (link == 0 || insn_cost (tmp, link, last_scheduled_insn) == 1)
  2365.     tmp_class = 3;
  2366.       else if (REG_NOTE_KIND (link) == 0) /* Data dependence.  */
  2367.     tmp_class = 1;
  2368.       else
  2369.     tmp_class = 2;
  2370.  
  2371.       link = find_insn_list (tmp2, LOG_LINKS (last_scheduled_insn));
  2372.       if (link == 0 || insn_cost (tmp2, link, last_scheduled_insn) == 1)
  2373.     tmp2_class = 3;
  2374.       else if (REG_NOTE_KIND (link) == 0) /* Data dependence.  */
  2375.     tmp2_class = 1;
  2376.       else
  2377.     tmp2_class = 2;
  2378.  
  2379.       if (value = tmp_class - tmp2_class)
  2380.     return value;
  2381.     }
  2382.  
  2383.   /* If insns are equally good, sort by INSN_LUID (original insn order),
  2384.      so that we make the sort stable.  This minimizes instruction movement,
  2385.      thus minimizing sched's effect on debugging and cross-jumping.  */
  2386.   return INSN_LUID (tmp) - INSN_LUID (tmp2);
  2387. }
  2388.  
  2389. /* Resort the array A in which only element at index N may be out of order.  */
  2390.  
  2391. __inline static void
  2392. swap_sort (a, n)
  2393.      rtx *a;
  2394.      int n;
  2395. {
  2396.   rtx insn = a[n-1];
  2397.   int i = n-2;
  2398.  
  2399.   while (i >= 0 && rank_for_schedule (a+i, &insn) >= 0)
  2400.     {
  2401.       a[i+1] = a[i];
  2402.       i -= 1;
  2403.     }
  2404.   a[i+1] = insn;
  2405. }
  2406.  
  2407. static int max_priority;
  2408.  
  2409. /* Add INSN to the insn queue so that it fires at least N_CYCLES
  2410.    before the currently executing insn.  */
  2411.  
  2412. __inline static void
  2413. queue_insn (insn, n_cycles)
  2414.      rtx insn;
  2415.      int n_cycles;
  2416. {
  2417.   int next_q = NEXT_Q_AFTER (q_ptr, n_cycles);
  2418.   NEXT_INSN (insn) = insn_queue[next_q];
  2419.   insn_queue[next_q] = insn;
  2420.   q_size += 1;
  2421. }
  2422.  
  2423. /* Return nonzero if PAT is the pattern of an insn which makes a
  2424.    register live.  */
  2425.  
  2426. __inline static int
  2427. birthing_insn_p (pat)
  2428.      rtx pat;
  2429. {
  2430.   int j;
  2431.  
  2432.   if (reload_completed == 1)
  2433.     return 0;
  2434.  
  2435.   if (GET_CODE (pat) == SET
  2436.       && GET_CODE (SET_DEST (pat)) == REG)
  2437.     {
  2438.       rtx dest = SET_DEST (pat);
  2439.       int i = REGNO (dest);
  2440.       int offset = i / REGSET_ELT_BITS;
  2441.       REGSET_ELT_TYPE bit = (REGSET_ELT_TYPE) 1 << (i % REGSET_ELT_BITS);
  2442.  
  2443.       /* It would be more accurate to use refers_to_regno_p or
  2444.      reg_mentioned_p to determine when the dest is not live before this
  2445.      insn.  */
  2446.  
  2447.       if (bb_live_regs[offset] & bit)
  2448.     return (reg_n_sets[i] == 1);
  2449.  
  2450.       return 0;
  2451.     }
  2452.   if (GET_CODE (pat) == PARALLEL)
  2453.     {
  2454.       for (j = 0; j < XVECLEN (pat, 0); j++)
  2455.     if (birthing_insn_p (XVECEXP (pat, 0, j)))
  2456.       return 1;
  2457.     }
  2458.   return 0;
  2459. }
  2460.  
  2461. /* PREV is an insn that is ready to execute.  Adjust its priority if that
  2462.    will help shorten register lifetimes.  */
  2463.  
  2464. __inline static void
  2465. adjust_priority (prev)
  2466.      rtx prev;
  2467. {
  2468.   /* Trying to shorten register lives after reload has completed
  2469.      is useless and wrong.  It gives inaccurate schedules.  */
  2470.   if (reload_completed == 0)
  2471.     {
  2472.       rtx note;
  2473.       int n_deaths = 0;
  2474.  
  2475.       /* ??? This code has no effect, because REG_DEAD notes are removed
  2476.      before we ever get here.  */
  2477.       for (note = REG_NOTES (prev); note; note = XEXP (note, 1))
  2478.     if (REG_NOTE_KIND (note) == REG_DEAD)
  2479.       n_deaths += 1;
  2480.  
  2481.       /* Defer scheduling insns which kill registers, since that
  2482.      shortens register lives.  Prefer scheduling insns which
  2483.      make registers live for the same reason.  */
  2484.       switch (n_deaths)
  2485.     {
  2486.     default:
  2487.       INSN_PRIORITY (prev) >>= 3;
  2488.       break;
  2489.     case 3:
  2490.       INSN_PRIORITY (prev) >>= 2;
  2491.       break;
  2492.     case 2:
  2493.     case 1:
  2494.       INSN_PRIORITY (prev) >>= 1;
  2495.       break;
  2496.     case 0:
  2497.       if (birthing_insn_p (PATTERN (prev)))
  2498.         {
  2499.           int max = max_priority;
  2500.  
  2501.           if (max > INSN_PRIORITY (prev))
  2502.         INSN_PRIORITY (prev) = max;
  2503.         }
  2504.       break;
  2505.     }
  2506.     }
  2507. }
  2508.  
  2509. /* INSN is the "currently executing insn".  Launch each insn which was
  2510.    waiting on INSN (in the backwards dataflow sense).  READY is a
  2511.    vector of insns which are ready to fire.  N_READY is the number of
  2512.    elements in READY.  CLOCK is the current virtual cycle.  */
  2513.  
  2514. static int
  2515. schedule_insn (insn, ready, n_ready, clock)
  2516.      rtx insn;
  2517.      rtx *ready;
  2518.      int n_ready;
  2519.      int clock;
  2520. {
  2521.   rtx link;
  2522.   int new_ready = n_ready;
  2523.  
  2524.   if (MAX_BLOCKAGE > 1)
  2525.     schedule_unit (insn_unit (insn), insn, clock);
  2526.  
  2527.   if (LOG_LINKS (insn) == 0)
  2528.     return n_ready;
  2529.  
  2530.   /* This is used by the function adjust_priority above.  */
  2531.   if (n_ready > 0)
  2532.     max_priority = MAX (INSN_PRIORITY (ready[0]), INSN_PRIORITY (insn));
  2533.   else
  2534.     max_priority = INSN_PRIORITY (insn);
  2535.  
  2536.   for (link = LOG_LINKS (insn); link != 0; link = XEXP (link, 1))
  2537.     {
  2538.       rtx prev = XEXP (link, 0);
  2539.       int cost = insn_cost (prev, link, insn);
  2540.  
  2541.       if ((INSN_REF_COUNT (prev) -= 1) != 0)
  2542.     {
  2543.       /* We satisfied one requirement to fire PREV.  Record the earliest
  2544.          time when PREV can fire.  No need to do this if the cost is 1,
  2545.          because PREV can fire no sooner than the next cycle.  */
  2546.       if (cost > 1)
  2547.         INSN_TICK (prev) = MAX (INSN_TICK (prev), clock + cost);
  2548.     }
  2549.       else
  2550.     {
  2551.       /* We satisfied the last requirement to fire PREV.  Ensure that all
  2552.          timing requirements are satisfied.  */
  2553.       if (INSN_TICK (prev) - clock > cost)
  2554.         cost = INSN_TICK (prev) - clock;
  2555.  
  2556.       /* Adjust the priority of PREV and either put it on the ready
  2557.          list or queue it.  */
  2558.       adjust_priority (prev);
  2559.       if (cost <= 1)
  2560.         ready[new_ready++] = prev;
  2561.       else
  2562.         queue_insn (prev, cost);
  2563.     }
  2564.     }
  2565.  
  2566.   return new_ready;
  2567. }
  2568.  
  2569. /* Given N_READY insns in the ready list READY at time CLOCK, queue
  2570.    those that are blocked due to function unit hazards and rearrange
  2571.    the remaining ones to minimize subsequent function unit hazards.  */
  2572.  
  2573. static int
  2574. schedule_select (ready, n_ready, clock, file)
  2575.      rtx *ready;
  2576.      int n_ready, clock;
  2577.      FILE *file;
  2578. {
  2579.   int pri = INSN_PRIORITY (ready[0]);
  2580.   int i, j, k, q, cost, best_cost, best_insn = 0, new_ready = n_ready;
  2581.   rtx insn;
  2582.  
  2583.   /* Work down the ready list in groups of instructions with the same
  2584.      priority value.  Queue insns in the group that are blocked and
  2585.      select among those that remain for the one with the largest
  2586.      potential hazard.  */
  2587.   for (i = 0; i < n_ready; i = j)
  2588.     {
  2589.       int opri = pri;
  2590.       for (j = i + 1; j < n_ready; j++)
  2591.     if ((pri = INSN_PRIORITY (ready[j])) != opri)
  2592.       break;
  2593.  
  2594.       /* Queue insns in the group that are blocked.  */
  2595.       for (k = i, q = 0; k < j; k++)
  2596.     {
  2597.       insn = ready[k];
  2598.       if ((cost = actual_hazard (insn_unit (insn), insn, clock, 0)) != 0)
  2599.         {
  2600.           q++;
  2601.           ready[k] = 0;
  2602.           queue_insn (insn, cost);
  2603.           if (file)
  2604.         fprintf (file, "\n;; blocking insn %d for %d cycles",
  2605.              INSN_UID (insn), cost);
  2606.         }
  2607.     }
  2608.       new_ready -= q;
  2609.  
  2610.       /* Check the next group if all insns were queued.  */
  2611.       if (j - i - q == 0)
  2612.     continue;
  2613.  
  2614.       /* If more than one remains, select the first one with the largest
  2615.      potential hazard.  */
  2616.       else if (j - i - q > 1)
  2617.     {
  2618.       best_cost = -1;
  2619.       for (k = i; k < j; k++)
  2620.         {
  2621.           if ((insn = ready[k]) == 0)
  2622.         continue;
  2623.           if ((cost = potential_hazard (insn_unit (insn), insn, 0))
  2624.           > best_cost)
  2625.         {
  2626.           best_cost = cost;
  2627.           best_insn = k;
  2628.         }
  2629.         }
  2630.     }
  2631.       /* We have found a suitable insn to schedule.  */
  2632.       break;
  2633.     }
  2634.  
  2635.   /* Move the best insn to be front of the ready list.  */
  2636.   if (best_insn != 0)
  2637.     {
  2638.       if (file)
  2639.     {
  2640.       fprintf (file, ", now");
  2641.       for (i = 0; i < n_ready; i++)
  2642.         if (ready[i])
  2643.           fprintf (file, " %d", INSN_UID (ready[i]));
  2644.       fprintf (file, "\n;; insn %d has a greater potential hazard",
  2645.            INSN_UID (ready[best_insn]));
  2646.     }
  2647.       for (i = best_insn; i > 0; i--)
  2648.     {
  2649.       insn = ready[i-1];
  2650.       ready[i-1] = ready[i];
  2651.       ready[i] = insn;
  2652.     }
  2653.     }
  2654.  
  2655.   /* Compact the ready list.  */
  2656.   if (new_ready < n_ready)
  2657.     for (i = j = 0; i < n_ready; i++)
  2658.       if (ready[i])
  2659.     ready[j++] = ready[i];
  2660.  
  2661.   return new_ready;
  2662. }
  2663.  
  2664. /* Add a REG_DEAD note for REG to INSN, reusing a REG_DEAD note from the
  2665.    dead_notes list.  */
  2666.  
  2667. static void
  2668. create_reg_dead_note (reg, insn)
  2669.      rtx reg, insn;
  2670. {
  2671.   rtx link;
  2672.         
  2673.   /* The number of registers killed after scheduling must be the same as the
  2674.      number of registers killed before scheduling.  The number of REG_DEAD
  2675.      notes may not be conserved, i.e. two SImode hard register REG_DEAD notes
  2676.      might become one DImode hard register REG_DEAD note, but the number of
  2677.      registers killed will be conserved.
  2678.      
  2679.      We carefully remove REG_DEAD notes from the dead_notes list, so that
  2680.      there will be none left at the end.  If we run out early, then there
  2681.      is a bug somewhere in flow, combine and/or sched.  */
  2682.  
  2683.   if (dead_notes == 0)
  2684.     {
  2685. #if 1
  2686.       abort ();
  2687. #else
  2688.       link = rtx_alloc (EXPR_LIST);
  2689.       PUT_REG_NOTE_KIND (link, REG_DEAD);
  2690. #endif
  2691.     }
  2692.   else
  2693.     {
  2694.       /* Number of regs killed by REG.  */
  2695.       int regs_killed = (REGNO (reg) >= FIRST_PSEUDO_REGISTER ? 1
  2696.              : HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg)));
  2697.       /* Number of regs killed by REG_DEAD notes taken off the list.  */
  2698.       int reg_note_regs;
  2699.  
  2700.       link = dead_notes;
  2701.       reg_note_regs = (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
  2702.                : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
  2703.                        GET_MODE (XEXP (link, 0))));
  2704.       while (reg_note_regs < regs_killed)
  2705.     {
  2706.       link = XEXP (link, 1);
  2707.       reg_note_regs += (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
  2708.                 : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
  2709.                         GET_MODE (XEXP (link, 0))));
  2710.     }
  2711.       dead_notes = XEXP (link, 1);
  2712.  
  2713.       /* If we took too many regs kills off, put the extra ones back.  */
  2714.       while (reg_note_regs > regs_killed)
  2715.     {
  2716.       rtx temp_reg, temp_link;
  2717.  
  2718.       temp_reg = gen_rtx (REG, word_mode, 0);
  2719.       temp_link = rtx_alloc (EXPR_LIST);
  2720.       PUT_REG_NOTE_KIND (temp_link, REG_DEAD);
  2721.       XEXP (temp_link, 0) = temp_reg;
  2722.       XEXP (temp_link, 1) = dead_notes;
  2723.       dead_notes = temp_link;
  2724.       reg_note_regs--;
  2725.     }
  2726.     }
  2727.  
  2728.   XEXP (link, 0) = reg;
  2729.   XEXP (link, 1) = REG_NOTES (insn);
  2730.   REG_NOTES (insn) = link;
  2731. }
  2732.  
  2733. /* Subroutine on attach_deaths_insn--handles the recursive search
  2734.    through INSN.  If SET_P is true, then x is being modified by the insn.  */
  2735.  
  2736. static void
  2737. attach_deaths (x, insn, set_p)
  2738.      rtx x;
  2739.      rtx insn;
  2740.      int set_p;
  2741. {
  2742.   register int i;
  2743.   register int j;
  2744.   register enum rtx_code code;
  2745.   register char *fmt;
  2746.  
  2747.   if (x == 0)
  2748.     return;
  2749.  
  2750.   code = GET_CODE (x);
  2751.  
  2752.   switch (code)
  2753.     {
  2754.     case CONST_INT:
  2755.     case CONST_DOUBLE:
  2756.     case LABEL_REF:
  2757.     case SYMBOL_REF:
  2758.     case CONST:
  2759.     case CODE_LABEL:
  2760.     case PC:
  2761.     case CC0:
  2762.       /* Get rid of the easy cases first.  */
  2763.       return;
  2764.  
  2765.     case REG:
  2766.       {
  2767.     /* If the register dies in this insn, queue that note, and mark
  2768.        this register as needing to die.  */
  2769.     /* This code is very similar to mark_used_1 (if set_p is false)
  2770.        and mark_set_1 (if set_p is true) in flow.c.  */
  2771.  
  2772.     register int regno = REGNO (x);
  2773.     register int offset = regno / REGSET_ELT_BITS;
  2774.     register REGSET_ELT_TYPE bit
  2775.       = (REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS);
  2776.     REGSET_ELT_TYPE all_needed = (old_live_regs[offset] & bit);
  2777.     REGSET_ELT_TYPE some_needed = (old_live_regs[offset] & bit);
  2778.  
  2779.     if (set_p)
  2780.       return;
  2781.  
  2782.     if (regno < FIRST_PSEUDO_REGISTER)
  2783.       {
  2784.         int n;
  2785.  
  2786.         n = HARD_REGNO_NREGS (regno, GET_MODE (x));
  2787.         while (--n > 0)
  2788.           {
  2789.         some_needed |= (old_live_regs[(regno + n) / REGSET_ELT_BITS]
  2790.                 & ((REGSET_ELT_TYPE) 1
  2791.                    << ((regno + n) % REGSET_ELT_BITS)));
  2792.         all_needed &= (old_live_regs[(regno + n) / REGSET_ELT_BITS]
  2793.                    & ((REGSET_ELT_TYPE) 1
  2794.                   << ((regno + n) % REGSET_ELT_BITS)));
  2795.           }
  2796.       }
  2797.  
  2798.     /* If it wasn't live before we started, then add a REG_DEAD note.
  2799.        We must check the previous lifetime info not the current info,
  2800.        because we may have to execute this code several times, e.g.
  2801.        once for a clobber (which doesn't add a note) and later
  2802.        for a use (which does add a note).
  2803.        
  2804.        Always make the register live.  We must do this even if it was
  2805.        live before, because this may be an insn which sets and uses
  2806.        the same register, in which case the register has already been
  2807.        killed, so we must make it live again.
  2808.  
  2809.        Global registers are always live, and should never have a REG_DEAD
  2810.        note added for them, so none of the code below applies to them.  */
  2811.  
  2812.     if (regno >= FIRST_PSEUDO_REGISTER || ! global_regs[regno])
  2813.       {
  2814.         /* Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
  2815.            STACK_POINTER_REGNUM, since these are always considered to be
  2816.            live.  Similarly for ARG_POINTER_REGNUM if it is fixed.  */
  2817.         if (regno != FRAME_POINTER_REGNUM
  2818. #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
  2819.         && ! (regno == HARD_FRAME_POINTER_REGNUM)
  2820. #endif
  2821. #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
  2822.         && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
  2823. #endif
  2824.         && regno != STACK_POINTER_REGNUM)
  2825.           {
  2826.         if (! all_needed && ! dead_or_set_p (insn, x))
  2827.           {
  2828.             /* If none of the words in X is needed, make a REG_DEAD
  2829.                note.  Otherwise, we must make partial REG_DEAD
  2830.                notes.  */
  2831.             if (! some_needed)
  2832.               create_reg_dead_note (x, insn);
  2833.             else
  2834.               {
  2835.             int i;
  2836.  
  2837.             /* Don't make a REG_DEAD note for a part of a
  2838.                register that is set in the insn.  */
  2839.             for (i = HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1;
  2840.                  i >= 0; i--)
  2841.               if ((old_live_regs[(regno + i) / REGSET_ELT_BITS]
  2842.                    & ((REGSET_ELT_TYPE) 1
  2843.                   << ((regno +i) % REGSET_ELT_BITS))) == 0
  2844.                   && ! dead_or_set_regno_p (insn, regno + i))
  2845.                 create_reg_dead_note (gen_rtx (REG,
  2846.                                reg_raw_mode[regno + i],
  2847.                                regno + i),
  2848.                           insn);
  2849.               }
  2850.           }
  2851.           }
  2852.  
  2853.         if (regno < FIRST_PSEUDO_REGISTER)
  2854.           {
  2855.         int j = HARD_REGNO_NREGS (regno, GET_MODE (x));
  2856.         while (--j >= 0)
  2857.           {
  2858.             offset = (regno + j) / REGSET_ELT_BITS;
  2859.             bit
  2860.               = (REGSET_ELT_TYPE) 1 << ((regno + j) % REGSET_ELT_BITS);
  2861.  
  2862.             bb_dead_regs[offset] &= ~bit;
  2863.             bb_live_regs[offset] |= bit;
  2864.           }
  2865.           }
  2866.         else
  2867.           {
  2868.         bb_dead_regs[offset] &= ~bit;
  2869.         bb_live_regs[offset] |= bit;
  2870.           }
  2871.       }
  2872.     return;
  2873.       }
  2874.  
  2875.     case MEM:
  2876.       /* Handle tail-recursive case.  */
  2877.       attach_deaths (XEXP (x, 0), insn, 0);
  2878.       return;
  2879.  
  2880.     case SUBREG:
  2881.     case STRICT_LOW_PART:
  2882.       /* These two cases preserve the value of SET_P, so handle them
  2883.      separately.  */
  2884.       attach_deaths (XEXP (x, 0), insn, set_p);
  2885.       return;
  2886.  
  2887.     case ZERO_EXTRACT:
  2888.     case SIGN_EXTRACT:
  2889.       /* This case preserves the value of SET_P for the first operand, but
  2890.      clears it for the other two.  */
  2891.       attach_deaths (XEXP (x, 0), insn, set_p);
  2892.       attach_deaths (XEXP (x, 1), insn, 0);
  2893.       attach_deaths (XEXP (x, 2), insn, 0);
  2894.       return;
  2895.  
  2896.     default:
  2897.       /* Other cases: walk the insn.  */
  2898.       fmt = GET_RTX_FORMAT (code);
  2899.       for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
  2900.     {
  2901.       if (fmt[i] == 'e')
  2902.         attach_deaths (XEXP (x, i), insn, 0);
  2903.       else if (fmt[i] == 'E')
  2904.         for (j = 0; j < XVECLEN (x, i); j++)
  2905.           attach_deaths (XVECEXP (x, i, j), insn, 0);
  2906.     }
  2907.     }
  2908. }
  2909.  
  2910. /* After INSN has executed, add register death notes for each register
  2911.    that is dead after INSN.  */
  2912.  
  2913. static void
  2914. attach_deaths_insn (insn)
  2915.      rtx insn;
  2916. {
  2917.   rtx x = PATTERN (insn);
  2918.   register RTX_CODE code = GET_CODE (x);
  2919.   rtx link;
  2920.  
  2921.   if (code == SET)
  2922.     {
  2923.       attach_deaths (SET_SRC (x), insn, 0);
  2924.  
  2925.       /* A register might die here even if it is the destination, e.g.
  2926.      it is the target of a volatile read and is otherwise unused.
  2927.      Hence we must always call attach_deaths for the SET_DEST.  */
  2928.       attach_deaths (SET_DEST (x), insn, 1);
  2929.     }
  2930.   else if (code == PARALLEL)
  2931.     {
  2932.       register int i;
  2933.       for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
  2934.     {
  2935.       code = GET_CODE (XVECEXP (x, 0, i));
  2936.       if (code == SET)
  2937.         {
  2938.           attach_deaths (SET_SRC (XVECEXP (x, 0, i)), insn, 0);
  2939.  
  2940.           attach_deaths (SET_DEST (XVECEXP (x, 0, i)), insn, 1);
  2941.         }
  2942.       /* Flow does not add REG_DEAD notes to registers that die in
  2943.          clobbers, so we can't either.  */
  2944.       else if (code != CLOBBER)
  2945.         attach_deaths (XVECEXP (x, 0, i), insn, 0);
  2946.     }
  2947.     }
  2948.   /* If this is a CLOBBER, only add REG_DEAD notes to registers inside a
  2949.      MEM being clobbered, just like flow.  */
  2950.   else if (code == CLOBBER && GET_CODE (XEXP (x, 0)) == MEM)
  2951.     attach_deaths (XEXP (XEXP (x, 0), 0), insn, 0);
  2952.   /* Otherwise don't add a death note to things being clobbered.  */
  2953.   else if (code != CLOBBER)
  2954.     attach_deaths (x, insn, 0);
  2955.  
  2956.   /* Make death notes for things used in the called function.  */
  2957.   if (GET_CODE (insn) == CALL_INSN)
  2958.     for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
  2959.       attach_deaths (XEXP (XEXP (link, 0), 0), insn,
  2960.              GET_CODE (XEXP (link, 0)) == CLOBBER);
  2961. }
  2962.  
  2963. /* Delete notes beginning with INSN and maybe put them in the chain
  2964.    of notes ended by NOTE_LIST.
  2965.    Returns the insn following the notes.  */
  2966.  
  2967. static rtx
  2968. unlink_notes (insn, tail)
  2969.      rtx insn, tail;
  2970. {
  2971.   rtx prev = PREV_INSN (insn);
  2972.  
  2973.   while (insn != tail && GET_CODE (insn) == NOTE)
  2974.     {
  2975.       rtx next = NEXT_INSN (insn);
  2976.       /* Delete the note from its current position.  */
  2977.       if (prev)
  2978.     NEXT_INSN (prev) = next;
  2979.       if (next)
  2980.     PREV_INSN (next) = prev;
  2981.  
  2982.       if (write_symbols != NO_DEBUG && NOTE_LINE_NUMBER (insn) > 0)
  2983.     /* Record line-number notes so they can be reused.  */
  2984.     LINE_NOTE (insn) = insn;
  2985.  
  2986.       /* Don't save away NOTE_INSN_SETJMPs, because they must remain
  2987.      immediately after the call they follow.  We use a fake
  2988.      (REG_DEAD (const_int -1)) note to remember them.  */
  2989.       else if (NOTE_LINE_NUMBER (insn) != NOTE_INSN_SETJMP)
  2990.     {
  2991.       /* Insert the note at the end of the notes list.  */
  2992.       PREV_INSN (insn) = note_list;
  2993.       if (note_list)
  2994.         NEXT_INSN (note_list) = insn;
  2995.       note_list = insn;
  2996.     }
  2997.  
  2998.       insn = next;
  2999.     }
  3000.   return insn;
  3001. }
  3002.  
  3003. /* Constructor for `sometimes' data structure.  */
  3004.  
  3005. static int
  3006. new_sometimes_live (regs_sometimes_live, offset, bit, sometimes_max)
  3007.      struct sometimes *regs_sometimes_live;
  3008.      int offset, bit;
  3009.      int sometimes_max;
  3010. {
  3011.   register struct sometimes *p;
  3012.   register int regno = offset * REGSET_ELT_BITS + bit;
  3013.  
  3014.   /* There should never be a register greater than max_regno here.  If there
  3015.      is, it means that a define_split has created a new pseudo reg.  This
  3016.      is not allowed, since there will not be flow info available for any
  3017.      new register, so catch the error here.  */
  3018.   if (regno >= max_regno)
  3019.     abort ();
  3020.  
  3021.   p = ®s_sometimes_live[sometimes_max];
  3022.   p->offset = offset;
  3023.   p->bit = bit;
  3024.   p->live_length = 0;
  3025.   p->calls_crossed = 0;
  3026.   sometimes_max++;
  3027.   return sometimes_max;
  3028. }
  3029.  
  3030. /* Count lengths of all regs we are currently tracking,
  3031.    and find new registers no longer live.  */
  3032.  
  3033. static void
  3034. finish_sometimes_live (regs_sometimes_live, sometimes_max)
  3035.      struct sometimes *regs_sometimes_live;
  3036.      int sometimes_max;
  3037. {
  3038.   int i;
  3039.  
  3040.   for (i = 0; i < sometimes_max; i++)
  3041.     {
  3042.       register struct sometimes *p = ®s_sometimes_live[i];
  3043.       int regno;
  3044.  
  3045.       regno = p->offset * REGSET_ELT_BITS + p->bit;
  3046.  
  3047.       sched_reg_live_length[regno] += p->live_length;
  3048.       sched_reg_n_calls_crossed[regno] += p->calls_crossed;
  3049.     }
  3050. }
  3051.  
  3052. /* Use modified list scheduling to rearrange insns in basic block
  3053.    B.  FILE, if nonzero, is where we dump interesting output about
  3054.    this pass.  */
  3055.  
  3056. static void
  3057. schedule_block (b, file)
  3058.      int b;
  3059.      FILE *file;
  3060. {
  3061.   rtx insn, last;
  3062.   rtx *ready, link;
  3063.   int i, j, n_ready = 0, new_ready, n_insns = 0;
  3064.   int sched_n_insns = 0;
  3065.   int clock;
  3066. #define NEED_NOTHING    0
  3067. #define NEED_HEAD    1
  3068. #define NEED_TAIL    2
  3069.   int new_needs;
  3070.  
  3071.   /* HEAD and TAIL delimit the region being scheduled.  */
  3072.   rtx head = basic_block_head[b];
  3073.   rtx tail = basic_block_end[b];
  3074.   /* PREV_HEAD and NEXT_TAIL are the boundaries of the insns
  3075.      being scheduled.  When the insns have been ordered,
  3076.      these insns delimit where the new insns are to be
  3077.      spliced back into the insn chain.  */
  3078.   rtx next_tail;
  3079.   rtx prev_head;
  3080.  
  3081.   /* Keep life information accurate.  */
  3082.   register struct sometimes *regs_sometimes_live;
  3083.   int sometimes_max;
  3084.  
  3085.   if (file)
  3086.     fprintf (file, ";;\t -- basic block number %d from %d to %d --\n",
  3087.          b, INSN_UID (basic_block_head[b]), INSN_UID (basic_block_end[b]));
  3088.  
  3089.   i = max_reg_num ();
  3090.   reg_last_uses = (rtx *) alloca (i * sizeof (rtx));
  3091.   bzero ((char *) reg_last_uses, i * sizeof (rtx));
  3092.   reg_last_sets = (rtx *) alloca (i * sizeof (rtx));
  3093.   bzero ((char *) reg_last_sets, i * sizeof (rtx));
  3094.   reg_pending_sets = (regset) alloca (regset_bytes);
  3095.   bzero ((char *) reg_pending_sets, regset_bytes);
  3096.   reg_pending_sets_all = 0;
  3097.   clear_units ();
  3098.  
  3099.   /* Remove certain insns at the beginning from scheduling,
  3100.      by advancing HEAD.  */
  3101.  
  3102.   /* At the start of a function, before reload has run, don't delay getting
  3103.      parameters from hard registers into pseudo registers.  */
  3104.   if (reload_completed == 0 && b == 0)
  3105.     {
  3106.       while (head != tail
  3107.          && GET_CODE (head) == NOTE
  3108.          && NOTE_LINE_NUMBER (head) != NOTE_INSN_FUNCTION_BEG)
  3109.     head = NEXT_INSN (head);
  3110.       while (head != tail
  3111.          && GET_CODE (head) == INSN
  3112.          && GET_CODE (PATTERN (head)) == SET)
  3113.     {
  3114.       rtx src = SET_SRC (PATTERN (head));
  3115.       while (GET_CODE (src) == SUBREG
  3116.          || GET_CODE (src) == SIGN_EXTEND
  3117.          || GET_CODE (src) == ZERO_EXTEND
  3118.          || GET_CODE (src) == SIGN_EXTRACT
  3119.          || GET_CODE (src) == ZERO_EXTRACT)
  3120.         src = XEXP (src, 0);
  3121.       if (GET_CODE (src) != REG
  3122.           || REGNO (src) >= FIRST_PSEUDO_REGISTER)
  3123.         break;
  3124.       /* Keep this insn from ever being scheduled.  */
  3125.       INSN_REF_COUNT (head) = 1;
  3126.       head = NEXT_INSN (head);
  3127.     }
  3128.     }
  3129.  
  3130.   /* Don't include any notes or labels at the beginning of the
  3131.      basic block, or notes at the ends of basic blocks.  */
  3132.   while (head != tail)
  3133.     {
  3134.       if (GET_CODE (head) == NOTE)
  3135.     head = NEXT_INSN (head);
  3136.       else if (GET_CODE (tail) == NOTE)
  3137.     tail = PREV_INSN (tail);
  3138.       else if (GET_CODE (head) == CODE_LABEL)
  3139.     head = NEXT_INSN (head);
  3140.       else break;
  3141.     }
  3142.   /* If the only insn left is a NOTE or a CODE_LABEL, then there is no need
  3143.      to schedule this block.  */
  3144.   if (head == tail
  3145.       && (GET_CODE (head) == NOTE || GET_CODE (head) == CODE_LABEL))
  3146.     return;
  3147.  
  3148. #if 0
  3149.   /* This short-cut doesn't work.  It does not count call insns crossed by
  3150.      registers in reg_sometimes_live.  It does not mark these registers as
  3151.      dead if they die in this block.  It does not mark these registers live
  3152.      (or create new reg_sometimes_live entries if necessary) if they are born
  3153.      in this block.
  3154.  
  3155.      The easy solution is to just always schedule a block.  This block only
  3156.      has one insn, so this won't slow down this pass by much.  */
  3157.  
  3158.   if (head == tail)
  3159.     return;
  3160. #endif
  3161.  
  3162.   /* Now HEAD through TAIL are the insns actually to be rearranged;
  3163.      Let PREV_HEAD and NEXT_TAIL enclose them.  */
  3164.   prev_head = PREV_INSN (head);
  3165.   next_tail = NEXT_INSN (tail);
  3166.  
  3167.   /* Initialize basic block data structures.  */
  3168.   dead_notes = 0;
  3169.   pending_read_insns = 0;
  3170.   pending_read_mems = 0;
  3171.   pending_write_insns = 0;
  3172.   pending_write_mems = 0;
  3173.   pending_lists_length = 0;
  3174.   last_pending_memory_flush = 0;
  3175.   last_function_call = 0;
  3176.   last_scheduled_insn = 0;
  3177.  
  3178.   LOG_LINKS (sched_before_next_call) = 0;
  3179.  
  3180.   n_insns += sched_analyze (head, tail);
  3181.   if (n_insns == 0)
  3182.     {
  3183.       free_pending_lists ();
  3184.       return;
  3185.     }
  3186.  
  3187.   /* Allocate vector to hold insns to be rearranged (except those
  3188.      insns which are controlled by an insn with SCHED_GROUP_P set).
  3189.      All these insns are included between ORIG_HEAD and ORIG_TAIL,
  3190.      as those variables ultimately are set up.  */
  3191.   ready = (rtx *) alloca ((n_insns+1) * sizeof (rtx));
  3192.  
  3193.   /* TAIL is now the last of the insns to be rearranged.
  3194.      Put those insns into the READY vector.  */
  3195.   insn = tail;
  3196.  
  3197.   /* For all branches, calls, uses, and cc0 setters, force them to remain
  3198.      in order at the end of the block by adding dependencies and giving
  3199.      the last a high priority.  There may be notes present, and prev_head
  3200.      may also be a note.
  3201.  
  3202.      Branches must obviously remain at the end.  Calls should remain at the
  3203.      end since moving them results in worse register allocation.  Uses remain
  3204.      at the end to ensure proper register allocation.  cc0 setters remaim
  3205.      at the end because they can't be moved away from their cc0 user.  */
  3206.   last = 0;
  3207.   while (GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN
  3208.      || (GET_CODE (insn) == INSN
  3209.          && (GET_CODE (PATTERN (insn)) == USE
  3210. #ifdef HAVE_cc0
  3211.          || sets_cc0_p (PATTERN (insn))
  3212. #endif
  3213.          ))
  3214.      || GET_CODE (insn) == NOTE)
  3215.     {
  3216.       if (GET_CODE (insn) != NOTE)
  3217.     {
  3218.       priority (insn);
  3219.       if (last == 0)
  3220.         {
  3221.           ready[n_ready++] = insn;
  3222.           INSN_PRIORITY (insn) = TAIL_PRIORITY - i;
  3223.           INSN_REF_COUNT (insn) = 0;
  3224.         }
  3225.       else if (! find_insn_list (insn, LOG_LINKS (last)))
  3226.         {
  3227.           add_dependence (last, insn, REG_DEP_ANTI);
  3228.           INSN_REF_COUNT (insn)++;
  3229.         }
  3230.       last = insn;
  3231.  
  3232.       /* Skip over insns that are part of a group.  */
  3233.       while (SCHED_GROUP_P (insn))
  3234.         {
  3235.           insn = prev_nonnote_insn (insn);
  3236.           priority (insn);
  3237.         }
  3238.     }
  3239.  
  3240.       insn = PREV_INSN (insn);
  3241.       /* Don't overrun the bounds of the basic block.  */
  3242.       if (insn == prev_head)
  3243.     break;
  3244.     }
  3245.  
  3246.   /* Assign priorities to instructions.  Also check whether they
  3247.      are in priority order already.  If so then I will be nonnegative.
  3248.      We use this shortcut only before reloading.  */
  3249. #if 0
  3250.   i = reload_completed ? DONE_PRIORITY : MAX_PRIORITY;
  3251. #endif
  3252.  
  3253.   for (; insn != prev_head; insn = PREV_INSN (insn))
  3254.     {
  3255.       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
  3256.     {
  3257.       priority (insn);
  3258.       if (INSN_REF_COUNT (insn) == 0)
  3259.         {
  3260.           if (last == 0)
  3261.         ready[n_ready++] = insn;
  3262.           else
  3263.         {
  3264.           /* Make this dependent on the last of the instructions
  3265.              that must remain in order at the end of the block.  */
  3266.           add_dependence (last, insn, REG_DEP_ANTI);
  3267.           INSN_REF_COUNT (insn) = 1;
  3268.         }
  3269.         }
  3270.       if (SCHED_GROUP_P (insn))
  3271.         {
  3272.           while (SCHED_GROUP_P (insn))
  3273.         {
  3274.           insn = PREV_INSN (insn);
  3275.           while (GET_CODE (insn) == NOTE)
  3276.             insn = PREV_INSN (insn);
  3277.           priority (insn);
  3278.         }
  3279.           continue;
  3280.         }
  3281. #if 0
  3282.       if (i < 0)
  3283.         continue;
  3284.       if (INSN_PRIORITY (insn) < i)
  3285.         i = INSN_PRIORITY (insn);
  3286.       else if (INSN_PRIORITY (insn) > i)
  3287.         i = DONE_PRIORITY;
  3288. #endif
  3289.     }
  3290.     }
  3291.  
  3292. #if 0
  3293.   /* This short-cut doesn't work.  It does not count call insns crossed by
  3294.      registers in reg_sometimes_live.  It does not mark these registers as
  3295.      dead if they die in this block.  It does not mark these registers live
  3296.      (or create new reg_sometimes_live entries if necessary) if they are born
  3297.      in this block.
  3298.  
  3299.      The easy solution is to just always schedule a block.  These blocks tend
  3300.      to be very short, so this doesn't slow down this pass by much.  */
  3301.  
  3302.   /* If existing order is good, don't bother to reorder.  */
  3303.   if (i != DONE_PRIORITY)
  3304.     {
  3305.       if (file)
  3306.     fprintf (file, ";; already scheduled\n");
  3307.  
  3308.       if (reload_completed == 0)
  3309.     {
  3310.       for (i = 0; i < sometimes_max; i++)
  3311.         regs_sometimes_live[i].live_length += n_insns;
  3312.  
  3313.       finish_sometimes_live (regs_sometimes_live, sometimes_max);
  3314.     }
  3315.       free_pending_lists ();
  3316.       return;
  3317.     }
  3318. #endif
  3319.  
  3320.   /* Scan all the insns to be scheduled, removing NOTE insns
  3321.      and register death notes.
  3322.      Line number NOTE insns end up in NOTE_LIST.
  3323.      Register death notes end up in DEAD_NOTES.
  3324.  
  3325.      Recreate the register life information for the end of this basic
  3326.      block.  */
  3327.  
  3328.   if (reload_completed == 0)
  3329.     {
  3330.       bcopy ((char *) basic_block_live_at_start[b], (char *) bb_live_regs,
  3331.          regset_bytes);
  3332.       bzero ((char *) bb_dead_regs, regset_bytes);
  3333.  
  3334.       if (b == 0)
  3335.     {
  3336.       /* This is the first block in the function.  There may be insns
  3337.          before head that we can't schedule.   We still need to examine
  3338.          them though for accurate register lifetime analysis.  */
  3339.  
  3340.       /* We don't want to remove any REG_DEAD notes as the code below
  3341.          does.  */
  3342.  
  3343.       for (insn = basic_block_head[b]; insn != head;
  3344.            insn = NEXT_INSN (insn))
  3345.         if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
  3346.           {
  3347.         /* See if the register gets born here.  */
  3348.         /* We must check for registers being born before we check for
  3349.            registers dying.  It is possible for a register to be born
  3350.            and die in the same insn, e.g. reading from a volatile
  3351.            memory location into an otherwise unused register.  Such
  3352.            a register must be marked as dead after this insn.  */
  3353.         if (GET_CODE (PATTERN (insn)) == SET
  3354.             || GET_CODE (PATTERN (insn)) == CLOBBER)
  3355.           sched_note_set (b, PATTERN (insn), 0);
  3356.         else if (GET_CODE (PATTERN (insn)) == PARALLEL)
  3357.           {
  3358.             int j;
  3359.             for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
  3360.               if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
  3361.               || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
  3362.             sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 0);
  3363.  
  3364.             /* ??? This code is obsolete and should be deleted.  It
  3365.                is harmless though, so we will leave it in for now.  */
  3366.             for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
  3367.               if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == USE)
  3368.             sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 0);
  3369.           }
  3370.  
  3371.         for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
  3372.           {
  3373.             if ((REG_NOTE_KIND (link) == REG_DEAD
  3374.              || REG_NOTE_KIND (link) == REG_UNUSED)
  3375.             /* Verify that the REG_NOTE has a legal value.  */
  3376.             && GET_CODE (XEXP (link, 0)) == REG)
  3377.               {
  3378.             register int regno = REGNO (XEXP (link, 0));
  3379.             register int offset = regno / REGSET_ELT_BITS;
  3380.             register REGSET_ELT_TYPE bit
  3381.               = (REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS);
  3382.  
  3383.             if (regno < FIRST_PSEUDO_REGISTER)
  3384.               {
  3385.                 int j = HARD_REGNO_NREGS (regno,
  3386.                               GET_MODE (XEXP (link, 0)));
  3387.                 while (--j >= 0)
  3388.                   {
  3389.                 offset = (regno + j) / REGSET_ELT_BITS;
  3390.                 bit = ((REGSET_ELT_TYPE) 1
  3391.                        << ((regno + j) % REGSET_ELT_BITS));
  3392.  
  3393.                 bb_live_regs[offset] &= ~bit;
  3394.                 bb_dead_regs[offset] |= bit;
  3395.                   }
  3396.               }
  3397.             else
  3398.               {
  3399.                 bb_live_regs[offset] &= ~bit;
  3400.                 bb_dead_regs[offset] |= bit;
  3401.               }
  3402.               }
  3403.           }
  3404.           }
  3405.     }
  3406.     }
  3407.  
  3408.   /* If debugging information is being produced, keep track of the line
  3409.      number notes for each insn.  */
  3410.   if (write_symbols != NO_DEBUG)
  3411.     {
  3412.       /* We must use the true line number for the first insn in the block
  3413.      that was computed and saved at the start of this pass.  We can't
  3414.      use the current line number, because scheduling of the previous
  3415.      block may have changed the current line number.  */
  3416.       rtx line = line_note_head[b];
  3417.  
  3418.       for (insn = basic_block_head[b];
  3419.        insn != next_tail;
  3420.        insn = NEXT_INSN (insn))
  3421.     if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
  3422.       line = insn;
  3423.     else
  3424.       LINE_NOTE (insn) = line;
  3425.     }
  3426.  
  3427.   for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
  3428.     {
  3429.       rtx prev, next, link;
  3430.  
  3431.       /* Farm out notes.  This is needed to keep the debugger from
  3432.      getting completely deranged.  */
  3433.       if (GET_CODE (insn) == NOTE)
  3434.     {
  3435.       prev = insn;
  3436.       insn = unlink_notes (insn, next_tail);
  3437.       if (prev == tail)
  3438.         abort ();
  3439.       if (prev == head)
  3440.         abort ();
  3441.       if (insn == next_tail)
  3442.         abort ();
  3443.     }
  3444.  
  3445.       if (reload_completed == 0
  3446.       && GET_RTX_CLASS (GET_CODE (insn)) == 'i')
  3447.     {
  3448.       /* See if the register gets born here.  */
  3449.       /* We must check for registers being born before we check for
  3450.          registers dying.  It is possible for a register to be born and
  3451.          die in the same insn, e.g. reading from a volatile memory
  3452.          location into an otherwise unused register.  Such a register
  3453.          must be marked as dead after this insn.  */
  3454.       if (GET_CODE (PATTERN (insn)) == SET
  3455.           || GET_CODE (PATTERN (insn)) == CLOBBER)
  3456.         sched_note_set (b, PATTERN (insn), 0);
  3457.       else if (GET_CODE (PATTERN (insn)) == PARALLEL)
  3458.         {
  3459.           int j;
  3460.           for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
  3461.         if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
  3462.             || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
  3463.           sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 0);
  3464.  
  3465.           /* ??? This code is obsolete and should be deleted.  It
  3466.          is harmless though, so we will leave it in for now.  */
  3467.           for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
  3468.         if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == USE)
  3469.           sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 0);
  3470.         }
  3471.  
  3472.       /* Need to know what registers this insn kills.  */
  3473.       for (prev = 0, link = REG_NOTES (insn); link; link = next)
  3474.         {
  3475.           next = XEXP (link, 1);
  3476.           if ((REG_NOTE_KIND (link) == REG_DEAD
  3477.            || REG_NOTE_KIND (link) == REG_UNUSED)
  3478.           /* Verify that the REG_NOTE has a legal value.  */
  3479.           && GET_CODE (XEXP (link, 0)) == REG)
  3480.         {
  3481.           register int regno = REGNO (XEXP (link, 0));
  3482.           register int offset = regno / REGSET_ELT_BITS;
  3483.           register REGSET_ELT_TYPE bit
  3484.             = (REGSET_ELT_TYPE) 1 << (regno % REGSET_ELT_BITS);
  3485.  
  3486.           /* Only unlink REG_DEAD notes; leave REG_UNUSED notes
  3487.              alone.  */
  3488.           if (REG_NOTE_KIND (link) == REG_DEAD)
  3489.             {
  3490.               if (prev)
  3491.             XEXP (prev, 1) = next;
  3492.               else
  3493.             REG_NOTES (insn) = next;
  3494.               XEXP (link, 1) = dead_notes;
  3495.               dead_notes = link;
  3496.             }
  3497.           else
  3498.             prev = link;
  3499.  
  3500.           if (regno < FIRST_PSEUDO_REGISTER)
  3501.             {
  3502.               int j = HARD_REGNO_NREGS (regno,
  3503.                         GET_MODE (XEXP (link, 0)));
  3504.               while (--j >= 0)
  3505.             {
  3506.               offset = (regno + j) / REGSET_ELT_BITS;
  3507.               bit = ((REGSET_ELT_TYPE) 1
  3508.                  << ((regno + j) % REGSET_ELT_BITS));
  3509.  
  3510.               bb_live_regs[offset] &= ~bit;
  3511.               bb_dead_regs[offset] |= bit;
  3512.             }
  3513.             }
  3514.           else
  3515.             {
  3516.               bb_live_regs[offset] &= ~bit;
  3517.               bb_dead_regs[offset] |= bit;
  3518.             }
  3519.         }
  3520.           else
  3521.         prev = link;
  3522.         }
  3523.     }
  3524.     }
  3525.  
  3526.   if (reload_completed == 0)
  3527.     {
  3528.       /* Keep track of register lives.  */
  3529.       old_live_regs = (regset) alloca (regset_bytes);
  3530.       regs_sometimes_live
  3531.     = (struct sometimes *) alloca (max_regno * sizeof (struct sometimes));
  3532.       sometimes_max = 0;
  3533.  
  3534.       /* Start with registers live at end.  */
  3535.       for (j = 0; j < regset_size; j++)
  3536.     {
  3537.       REGSET_ELT_TYPE live = bb_live_regs[j];
  3538.       old_live_regs[j] = live;
  3539.       if (live)
  3540.         {
  3541.           register int bit;
  3542.           for (bit = 0; bit < REGSET_ELT_BITS; bit++)
  3543.         if (live & ((REGSET_ELT_TYPE) 1 << bit))
  3544.           sometimes_max = new_sometimes_live (regs_sometimes_live, j,
  3545.                               bit, sometimes_max);
  3546.         }
  3547.     }
  3548.     }
  3549.  
  3550.   SCHED_SORT (ready, n_ready, 1);
  3551.  
  3552.   if (file)
  3553.     {
  3554.       fprintf (file, ";; ready list initially:\n;; ");
  3555.       for (i = 0; i < n_ready; i++)
  3556.     fprintf (file, "%d ", INSN_UID (ready[i]));
  3557.       fprintf (file, "\n\n");
  3558.  
  3559.       for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
  3560.     if (INSN_PRIORITY (insn) > 0)
  3561.       fprintf (file, ";; insn[%4d]: priority = %4d, ref_count = %4d\n",
  3562.            INSN_UID (insn), INSN_PRIORITY (insn),
  3563.            INSN_REF_COUNT (insn));
  3564.     }
  3565.  
  3566.   /* Now HEAD and TAIL are going to become disconnected
  3567.      entirely from the insn chain.  */
  3568.   tail = 0;
  3569.  
  3570.   /* Q_SIZE will always be zero here.  */
  3571.   q_ptr = 0; clock = 0;
  3572.   bzero ((char *) insn_queue, sizeof (insn_queue));
  3573.  
  3574.   /* Now, perform list scheduling.  */
  3575.  
  3576.   /* Where we start inserting insns is after TAIL.  */
  3577.   last = next_tail;
  3578.  
  3579.   new_needs = (NEXT_INSN (prev_head) == basic_block_head[b]
  3580.            ? NEED_HEAD : NEED_NOTHING);
  3581.   if (PREV_INSN (next_tail) == basic_block_end[b])
  3582.     new_needs |= NEED_TAIL;
  3583.  
  3584.   new_ready = n_ready;
  3585.   while (sched_n_insns < n_insns)
  3586.     {
  3587.       q_ptr = NEXT_Q (q_ptr); clock++;
  3588.  
  3589.       /* Add all pending insns that can be scheduled without stalls to the
  3590.      ready list.  */
  3591.       for (insn = insn_queue[q_ptr]; insn; insn = NEXT_INSN (insn))
  3592.     {
  3593.       if (file)
  3594.         fprintf (file, ";; launching %d before %d with no stalls at T-%d\n",
  3595.              INSN_UID (insn), INSN_UID (last), clock);
  3596.       ready[new_ready++] = insn;
  3597.       q_size -= 1;
  3598.     }
  3599.       insn_queue[q_ptr] = 0;
  3600.  
  3601.       /* If there are no ready insns, stall until one is ready and add all
  3602.      of the pending insns at that point to the ready list.  */
  3603.       if (new_ready == 0)
  3604.     {
  3605.       register int stalls;
  3606.  
  3607.       for (stalls = 1; stalls < INSN_QUEUE_SIZE; stalls++)
  3608.         if (insn = insn_queue[NEXT_Q_AFTER (q_ptr, stalls)])
  3609.           {
  3610.         for (; insn; insn = NEXT_INSN (insn))
  3611.           {
  3612.             if (file)
  3613.               fprintf (file, ";; launching %d before %d with %d stalls at T-%d\n",
  3614.                    INSN_UID (insn), INSN_UID (last), stalls, clock);
  3615.             ready[new_ready++] = insn;
  3616.             q_size -= 1;
  3617.           }
  3618.         insn_queue[NEXT_Q_AFTER (q_ptr, stalls)] = 0;
  3619.         break;
  3620.           }
  3621.  
  3622.       q_ptr = NEXT_Q_AFTER (q_ptr, stalls); clock += stalls;
  3623.     }
  3624.  
  3625.       /* There should be some instructions waiting to fire.  */
  3626.       if (new_ready == 0)
  3627.     abort ();
  3628.  
  3629.       if (file)
  3630.     {
  3631.       fprintf (file, ";; ready list at T-%d:", clock);
  3632.       for (i = 0; i < new_ready; i++)
  3633.         fprintf (file, " %d (%x)",
  3634.              INSN_UID (ready[i]), INSN_PRIORITY (ready[i]));
  3635.     }
  3636.  
  3637.       /* Sort the ready list and choose the best insn to schedule.  Select
  3638.      which insn should issue in this cycle and queue those that are
  3639.      blocked by function unit hazards.
  3640.  
  3641.      N_READY holds the number of items that were scheduled the last time,
  3642.      minus the one instruction scheduled on the last loop iteration; it
  3643.      is not modified for any other reason in this loop.  */
  3644.  
  3645.       SCHED_SORT (ready, new_ready, n_ready);
  3646.       if (MAX_BLOCKAGE > 1)
  3647.     {
  3648.       new_ready = schedule_select (ready, new_ready, clock, file);
  3649.       if (new_ready == 0)
  3650.         {
  3651.           if (file)
  3652.         fprintf (file, "\n");
  3653.           /* We must set n_ready here, to ensure that sorting always
  3654.          occurs when we come back to the SCHED_SORT line above.  */
  3655.           n_ready = 0;
  3656.           continue;
  3657.         }
  3658.     }
  3659.       n_ready = new_ready;
  3660.       last_scheduled_insn = insn = ready[0];
  3661.  
  3662.       /* The first insn scheduled becomes the new tail.  */
  3663.       if (tail == 0)
  3664.     tail = insn;
  3665.  
  3666.       if (file)
  3667.     {
  3668.       fprintf (file, ", now");
  3669.       for (i = 0; i < n_ready; i++)
  3670.         fprintf (file, " %d", INSN_UID (ready[i]));
  3671.       fprintf (file, "\n");
  3672.     }
  3673.  
  3674.       if (DONE_PRIORITY_P (insn))
  3675.     abort ();
  3676.  
  3677.       if (reload_completed == 0)
  3678.     {
  3679.       /* Process this insn, and each insn linked to this one which must
  3680.          be immediately output after this insn.  */
  3681.       do
  3682.         {
  3683.           /* First we kill registers set by this insn, and then we
  3684.          make registers used by this insn live.  This is the opposite
  3685.          order used above because we are traversing the instructions
  3686.          backwards.  */
  3687.  
  3688.           /* Strictly speaking, we should scan REG_UNUSED notes and make
  3689.          every register mentioned there live, however, we will just
  3690.          kill them again immediately below, so there doesn't seem to
  3691.          be any reason why we bother to do this.  */
  3692.  
  3693.           /* See if this is the last notice we must take of a register.  */
  3694.           if (GET_CODE (PATTERN (insn)) == SET
  3695.           || GET_CODE (PATTERN (insn)) == CLOBBER)
  3696.         sched_note_set (b, PATTERN (insn), 1);
  3697.           else if (GET_CODE (PATTERN (insn)) == PARALLEL)
  3698.         {
  3699.           int j;
  3700.           for (j = XVECLEN (PATTERN (insn), 0) - 1; j >= 0; j--)
  3701.             if (GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == SET
  3702.             || GET_CODE (XVECEXP (PATTERN (insn), 0, j)) == CLOBBER)
  3703.               sched_note_set (b, XVECEXP (PATTERN (insn), 0, j), 1);
  3704.         }
  3705.           
  3706.           /* This code keeps life analysis information up to date.  */
  3707.           if (GET_CODE (insn) == CALL_INSN)
  3708.         {
  3709.           register struct sometimes *p;
  3710.  
  3711.           /* A call kills all call used and global registers, except
  3712.              for those mentioned in the call pattern which will be
  3713.              made live again later.  */
  3714.           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
  3715.             if (call_used_regs[i] || global_regs[i])
  3716.               {
  3717.             register int offset = i / REGSET_ELT_BITS;
  3718.             register REGSET_ELT_TYPE bit
  3719.               = (REGSET_ELT_TYPE) 1 << (i % REGSET_ELT_BITS);
  3720.  
  3721.             bb_live_regs[offset] &= ~bit;
  3722.             bb_dead_regs[offset] |= bit;
  3723.               }
  3724.  
  3725.           /* Regs live at the time of a call instruction must not
  3726.              go in a register clobbered by calls.  Record this for
  3727.              all regs now live.  Note that insns which are born or
  3728.              die in a call do not cross a call, so this must be done
  3729.              after the killings (above) and before the births
  3730.              (below).  */
  3731.           p = regs_sometimes_live;
  3732.           for (i = 0; i < sometimes_max; i++, p++)
  3733.             if (bb_live_regs[p->offset]
  3734.             & ((REGSET_ELT_TYPE) 1 << p->bit))
  3735.               p->calls_crossed += 1;
  3736.         }
  3737.  
  3738.           /* Make every register used live, and add REG_DEAD notes for
  3739.          registers which were not live before we started.  */
  3740.           attach_deaths_insn (insn);
  3741.  
  3742.           /* Find registers now made live by that instruction.  */
  3743.           for (i = 0; i < regset_size; i++)
  3744.         {
  3745.           REGSET_ELT_TYPE diff = bb_live_regs[i] & ~old_live_regs[i];
  3746.           if (diff)
  3747.             {
  3748.               register int bit;
  3749.               old_live_regs[i] |= diff;
  3750.               for (bit = 0; bit < REGSET_ELT_BITS; bit++)
  3751.             if (diff & ((REGSET_ELT_TYPE) 1 << bit))
  3752.               sometimes_max
  3753.                 = new_sometimes_live (regs_sometimes_live, i, bit,
  3754.                           sometimes_max);
  3755.             }
  3756.         }
  3757.  
  3758.           /* Count lengths of all regs we are worrying about now,
  3759.          and handle registers no longer live.  */
  3760.  
  3761.           for (i = 0; i < sometimes_max; i++)
  3762.         {
  3763.           register struct sometimes *p = ®s_sometimes_live[i];
  3764.           int regno = p->offset*REGSET_ELT_BITS + p->bit;
  3765.  
  3766.           p->live_length += 1;
  3767.  
  3768.           if ((bb_live_regs[p->offset]
  3769.                & ((REGSET_ELT_TYPE) 1 << p->bit)) == 0)
  3770.             {
  3771.               /* This is the end of one of this register's lifetime
  3772.              segments.  Save the lifetime info collected so far,
  3773.              and clear its bit in the old_live_regs entry.  */
  3774.               sched_reg_live_length[regno] += p->live_length;
  3775.               sched_reg_n_calls_crossed[regno] += p->calls_crossed;
  3776.               old_live_regs[p->offset]
  3777.             &= ~((REGSET_ELT_TYPE) 1 << p->bit);
  3778.  
  3779.               /* Delete the reg_sometimes_live entry for this reg by
  3780.              copying the last entry over top of it.  */
  3781.               *p = regs_sometimes_live[--sometimes_max];
  3782.               /* ...and decrement i so that this newly copied entry
  3783.              will be processed.  */
  3784.               i--;
  3785.             }
  3786.         }
  3787.  
  3788.           link = insn;
  3789.           insn = PREV_INSN (insn);
  3790.         }
  3791.       while (SCHED_GROUP_P (link));
  3792.  
  3793.       /* Set INSN back to the insn we are scheduling now.  */
  3794.       insn = ready[0];
  3795.     }
  3796.  
  3797.       /* Schedule INSN.  Remove it from the ready list.  */
  3798.       ready += 1;
  3799.       n_ready -= 1;
  3800.  
  3801.       sched_n_insns += 1;
  3802.       NEXT_INSN (insn) = last;
  3803.       PREV_INSN (last) = insn;
  3804.       last = insn;
  3805.  
  3806.       /* Check to see if we need to re-emit a NOTE_INSN_SETJMP here.  */
  3807.       if (GET_CODE (insn) == CALL_INSN)
  3808.     {
  3809.       rtx note = find_reg_note (insn, REG_DEAD, constm1_rtx);
  3810.  
  3811.       if (note)
  3812.         {
  3813.           emit_note_after (NOTE_INSN_SETJMP, insn);
  3814.           remove_note (insn, note);
  3815.         }
  3816.     }
  3817.  
  3818.       /* Everything that precedes INSN now either becomes "ready", if
  3819.      it can execute immediately before INSN, or "pending", if
  3820.      there must be a delay.  Give INSN high enough priority that
  3821.      at least one (maybe more) reg-killing insns can be launched
  3822.      ahead of all others.  Mark INSN as scheduled by changing its
  3823.      priority to -1.  */
  3824.       INSN_PRIORITY (insn) = LAUNCH_PRIORITY;
  3825.       new_ready = schedule_insn (insn, ready, n_ready, clock);
  3826.       INSN_PRIORITY (insn) = DONE_PRIORITY;
  3827.  
  3828.       /* Schedule all prior insns that must not be moved.  */
  3829.       if (SCHED_GROUP_P (insn))
  3830.     {
  3831.       /* Disable these insns from being launched.  */
  3832.       link = insn;
  3833.       while (SCHED_GROUP_P (link))
  3834.         {
  3835.           /* Disable these insns from being launched by anybody.  */
  3836.           link = PREV_INSN (link);
  3837.           INSN_REF_COUNT (link) = 0;
  3838.         }
  3839.  
  3840.       /* None of these insns can move forward into delay slots.  */
  3841.       while (SCHED_GROUP_P (insn))
  3842.         {
  3843.           insn = PREV_INSN (insn);
  3844.           new_ready = schedule_insn (insn, ready, new_ready, clock);
  3845.           INSN_PRIORITY (insn) = DONE_PRIORITY;
  3846.  
  3847.           sched_n_insns += 1;
  3848.           NEXT_INSN (insn) = last;
  3849.           PREV_INSN (last) = insn;
  3850.           last = insn;
  3851.         }
  3852.     }
  3853.     }
  3854.   if (q_size != 0)
  3855.     abort ();
  3856.  
  3857.   if (reload_completed == 0)
  3858.     finish_sometimes_live (regs_sometimes_live, sometimes_max);
  3859.  
  3860.   /* HEAD is now the first insn in the chain of insns that
  3861.      been scheduled by the loop above.
  3862.      TAIL is the last of those insns.  */
  3863.   head = insn;
  3864.  
  3865.   /* NOTE_LIST is the end of a chain of notes previously found
  3866.      among the insns.  Insert them at the beginning of the insns.  */
  3867.   if (note_list != 0)
  3868.     {
  3869.       rtx note_head = note_list;
  3870.       while (PREV_INSN (note_head))
  3871.     note_head = PREV_INSN (note_head);
  3872.  
  3873.       PREV_INSN (head) = note_list;
  3874.       NEXT_INSN (note_list) = head;
  3875.       head = note_head;
  3876.     }
  3877.  
  3878.   /* There should be no REG_DEAD notes leftover at the end.
  3879.      In practice, this can occur as the result of bugs in flow, combine.c,
  3880.      and/or sched.c.  The values of the REG_DEAD notes remaining are
  3881.      meaningless, because dead_notes is just used as a free list.  */
  3882. #if 1
  3883.   if (dead_notes != 0)
  3884.     abort ();
  3885. #endif
  3886.  
  3887.   if (new_needs & NEED_HEAD)
  3888.     basic_block_head[b] = head;
  3889.   PREV_INSN (head) = prev_head;
  3890.   NEXT_INSN (prev_head) = head;
  3891.  
  3892.   if (new_needs & NEED_TAIL)
  3893.     basic_block_end[b] = tail;
  3894.   NEXT_INSN (tail) = next_tail;
  3895.   PREV_INSN (next_tail) = tail;
  3896.  
  3897.   /* Restore the line-number notes of each insn.  */
  3898.   if (write_symbols != NO_DEBUG)
  3899.     {
  3900.       rtx line, note, prev, new;
  3901.       int notes = 0;
  3902.  
  3903.       head = basic_block_head[b];
  3904.       next_tail = NEXT_INSN (basic_block_end[b]);
  3905.  
  3906.       /* Determine the current line-number.  We want to know the current
  3907.      line number of the first insn of the block here, in case it is
  3908.      different from the true line number that was saved earlier.  If
  3909.      different, then we need a line number note before the first insn
  3910.      of this block.  If it happens to be the same, then we don't want to
  3911.      emit another line number note here.  */
  3912.       for (line = head; line; line = PREV_INSN (line))
  3913.     if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
  3914.       break;
  3915.  
  3916.       /* Walk the insns keeping track of the current line-number and inserting
  3917.      the line-number notes as needed.  */
  3918.       for (insn = head; insn != next_tail; insn = NEXT_INSN (insn))
  3919.     if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
  3920.       line = insn;
  3921.       /* This used to emit line number notes before every non-deleted note.
  3922.      However, this confuses a debugger, because line notes not separated
  3923.      by real instructions all end up at the same address.  I can find no
  3924.      use for line number notes before other notes, so none are emitted.  */
  3925.     else if (GET_CODE (insn) != NOTE
  3926.          && (note = LINE_NOTE (insn)) != 0
  3927.          && note != line
  3928.          && (line == 0
  3929.              || NOTE_LINE_NUMBER (note) != NOTE_LINE_NUMBER (line)
  3930.              || NOTE_SOURCE_FILE (note) != NOTE_SOURCE_FILE (line)))
  3931.       {
  3932.         line = note;
  3933.         prev = PREV_INSN (insn);
  3934.         if (LINE_NOTE (note))
  3935.           {
  3936.         /* Re-use the original line-number note. */
  3937.         LINE_NOTE (note) = 0;
  3938.         PREV_INSN (note) = prev;
  3939.         NEXT_INSN (prev) = note;
  3940.         PREV_INSN (insn) = note;
  3941.         NEXT_INSN (note) = insn;
  3942.           }
  3943.         else
  3944.           {
  3945.         notes++;
  3946.         new = emit_note_after (NOTE_LINE_NUMBER (note), prev);
  3947.         NOTE_SOURCE_FILE (new) = NOTE_SOURCE_FILE (note);
  3948.           }
  3949.       }
  3950.       if (file && notes)
  3951.     fprintf (file, ";; added %d line-number notes\n", notes);
  3952.     }
  3953.  
  3954.   if (file)
  3955.     {
  3956.       fprintf (file, ";; total time = %d\n;; new basic block head = %d\n;; new basic block end = %d\n\n",
  3957.            clock, INSN_UID (basic_block_head[b]), INSN_UID (basic_block_end[b]));
  3958.     }
  3959.  
  3960.   /* Yow! We're done!  */
  3961.   free_pending_lists ();
  3962.  
  3963.   return;
  3964. }
  3965.  
  3966. /* Subroutine of split_hard_reg_notes.  Searches X for any reference to
  3967.    REGNO, returning the rtx of the reference found if any.  Otherwise,
  3968.    returns 0.  */
  3969.  
  3970. static rtx
  3971. regno_use_in (regno, x)
  3972.      int regno;
  3973.      rtx x;
  3974. {
  3975.   register char *fmt;
  3976.   int i, j;
  3977.   rtx tem;
  3978.  
  3979.   if (GET_CODE (x) == REG && REGNO (x) == regno)
  3980.     return x;
  3981.  
  3982.   fmt = GET_RTX_FORMAT (GET_CODE (x));
  3983.   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
  3984.     {
  3985.       if (fmt[i] == 'e')
  3986.     {
  3987.       if (tem = regno_use_in (regno, XEXP (x, i)))
  3988.         return tem;
  3989.     }
  3990.       else if (fmt[i] == 'E')
  3991.     for (j = XVECLEN (x, i) - 1; j >= 0; j--)
  3992.       if (tem = regno_use_in (regno , XVECEXP (x, i, j)))
  3993.         return tem;
  3994.     }
  3995.  
  3996.   return 0;
  3997. }
  3998.  
  3999. /* Subroutine of update_flow_info.  Determines whether any new REG_NOTEs are
  4000.    needed for the hard register mentioned in the note.  This can happen
  4001.    if the reference to the hard register in the original insn was split into
  4002.    several smaller hard register references in the split insns.  */
  4003.  
  4004. static void
  4005. split_hard_reg_notes (note, first, last, orig_insn)
  4006.      rtx note, first, last, orig_insn;
  4007. {
  4008.   rtx reg, temp, link;
  4009.   int n_regs, i, new_reg;
  4010.   rtx insn;
  4011.  
  4012.   /* Assume that this is a REG_DEAD note.  */
  4013.   if (REG_NOTE_KIND (note) != REG_DEAD)
  4014.     abort ();
  4015.  
  4016.   reg = XEXP (note, 0);
  4017.  
  4018.   n_regs = HARD_REGNO_NREGS (REGNO (reg), GET_MODE (reg));
  4019.  
  4020.   for (i = 0; i < n_regs; i++)
  4021.     {
  4022.       new_reg = REGNO (reg) + i;
  4023.  
  4024.       /* Check for references to new_reg in the split insns.  */
  4025.       for (insn = last; ; insn = PREV_INSN (insn))
  4026.     {
  4027.       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
  4028.           && (temp = regno_use_in (new_reg, PATTERN (insn))))
  4029.         {
  4030.           /* Create a new reg dead note here.  */
  4031.           link = rtx_alloc (EXPR_LIST);
  4032.           PUT_REG_NOTE_KIND (link, REG_DEAD);
  4033.           XEXP (link, 0) = temp;
  4034.           XEXP (link, 1) = REG_NOTES (insn);
  4035.           REG_NOTES (insn) = link;
  4036.  
  4037.           /* If killed multiple registers here, then add in the excess.  */
  4038.           i += HARD_REGNO_NREGS (REGNO (temp), GET_MODE (temp)) - 1;
  4039.  
  4040.           break;
  4041.         }
  4042.       /* It isn't mentioned anywhere, so no new reg note is needed for
  4043.          this register.  */
  4044.       if (insn == first)
  4045.         break;
  4046.     }
  4047.     }
  4048. }
  4049.  
  4050. /* Subroutine of update_flow_info.  Determines whether a SET or CLOBBER in an
  4051.    insn created by splitting needs a REG_DEAD or REG_UNUSED note added.  */
  4052.  
  4053. static void
  4054. new_insn_dead_notes (pat, insn, last, orig_insn)
  4055.      rtx pat, insn, last, orig_insn;
  4056. {
  4057.   rtx dest, tem, set;
  4058.  
  4059.   /* PAT is either a CLOBBER or a SET here.  */
  4060.   dest = XEXP (pat, 0);
  4061.  
  4062.   while (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG
  4063.      || GET_CODE (dest) == STRICT_LOW_PART
  4064.      || GET_CODE (dest) == SIGN_EXTRACT)
  4065.     dest = XEXP (dest, 0);
  4066.  
  4067.   if (GET_CODE (dest) == REG)
  4068.     {
  4069.       for (tem = last; tem != insn; tem = PREV_INSN (tem))
  4070.     {
  4071.       if (GET_RTX_CLASS (GET_CODE (tem)) == 'i'
  4072.           && reg_overlap_mentioned_p (dest, PATTERN (tem))
  4073.           && (set = single_set (tem)))
  4074.         {
  4075.           rtx tem_dest = SET_DEST (set);
  4076.  
  4077.           while (GET_CODE (tem_dest) == ZERO_EXTRACT
  4078.              || GET_CODE (tem_dest) == SUBREG
  4079.              || GET_CODE (tem_dest) == STRICT_LOW_PART
  4080.              || GET_CODE (tem_dest) == SIGN_EXTRACT)
  4081.         tem_dest = XEXP (tem_dest, 0);
  4082.  
  4083.           if (! rtx_equal_p (tem_dest, dest))
  4084.         {
  4085.           /* Use the same scheme as combine.c, don't put both REG_DEAD
  4086.              and REG_UNUSED notes on the same insn.  */
  4087.           if (! find_regno_note (tem, REG_UNUSED, REGNO (dest))
  4088.               && ! find_regno_note (tem, REG_DEAD, REGNO (dest)))
  4089.             {
  4090.               rtx note = rtx_alloc (EXPR_LIST);
  4091.               PUT_REG_NOTE_KIND (note, REG_DEAD);
  4092.               XEXP (note, 0) = dest;
  4093.               XEXP (note, 1) = REG_NOTES (tem);
  4094.               REG_NOTES (tem) = note;
  4095.             }
  4096.           /* The reg only dies in one insn, the last one that uses
  4097.              it.  */
  4098.           break;
  4099.         }
  4100.           else if (reg_overlap_mentioned_p (dest, SET_SRC (set)))
  4101.         /* We found an instruction that both uses the register,
  4102.            and sets it, so no new REG_NOTE is needed for this set.  */
  4103.         break;
  4104.         }
  4105.     }
  4106.       /* If this is a set, it must die somewhere, unless it is the dest of
  4107.      the original insn, and hence is live after the original insn.  Abort
  4108.      if it isn't supposed to be live after the original insn.
  4109.  
  4110.      If this is a clobber, then just add a REG_UNUSED note.  */
  4111.       if (tem == insn)
  4112.     {
  4113.       int live_after_orig_insn = 0;
  4114.       rtx pattern = PATTERN (orig_insn);
  4115.       int i;
  4116.  
  4117.       if (GET_CODE (pat) == CLOBBER)
  4118.         {
  4119.           rtx note = rtx_alloc (EXPR_LIST);
  4120.           PUT_REG_NOTE_KIND (note, REG_UNUSED);
  4121.           XEXP (note, 0) = dest;
  4122.           XEXP (note, 1) = REG_NOTES (insn);
  4123.           REG_NOTES (insn) = note;
  4124.           return;
  4125.         }
  4126.  
  4127.       /* The original insn could have multiple sets, so search the
  4128.          insn for all sets.  */
  4129.       if (GET_CODE (pattern) == SET)
  4130.         {
  4131.           if (reg_overlap_mentioned_p (dest, SET_DEST (pattern)))
  4132.         live_after_orig_insn = 1;
  4133.         }
  4134.       else if (GET_CODE (pattern) == PARALLEL)
  4135.         {
  4136.           for (i = 0; i < XVECLEN (pattern, 0); i++)
  4137.         if (GET_CODE (XVECEXP (pattern, 0, i)) == SET
  4138.             && reg_overlap_mentioned_p (dest,
  4139.                         SET_DEST (XVECEXP (pattern,
  4140.                                    0, i))))
  4141.           live_after_orig_insn = 1;
  4142.         }
  4143.  
  4144.       if (! live_after_orig_insn)
  4145.         abort ();
  4146.     }
  4147.     }
  4148. }
  4149.  
  4150. /* Subroutine of update_flow_info.  Update the value of reg_n_sets for all
  4151.    registers modified by X.  INC is -1 if the containing insn is being deleted,
  4152.    and is 1 if the containing insn is a newly generated insn.  */
  4153.  
  4154. static void
  4155. update_n_sets (x, inc)
  4156.      rtx x;
  4157.      int inc;
  4158. {
  4159.   rtx dest = SET_DEST (x);
  4160.  
  4161.   while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SUBREG
  4162.      || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SIGN_EXTRACT)
  4163.     dest = SUBREG_REG (dest);
  4164.       
  4165.   if (GET_CODE (dest) == REG)
  4166.     {
  4167.       int regno = REGNO (dest);
  4168.       
  4169.       if (regno < FIRST_PSEUDO_REGISTER)
  4170.     {
  4171.       register int i;
  4172.       int endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (dest));
  4173.       
  4174.       for (i = regno; i < endregno; i++)
  4175.         reg_n_sets[i] += inc;
  4176.     }
  4177.       else
  4178.     reg_n_sets[regno] += inc;
  4179.     }
  4180. }
  4181.  
  4182. /* Updates all flow-analysis related quantities (including REG_NOTES) for
  4183.    the insns from FIRST to LAST inclusive that were created by splitting
  4184.    ORIG_INSN.  NOTES are the original REG_NOTES.  */
  4185.  
  4186. static void
  4187. update_flow_info (notes, first, last, orig_insn)
  4188.      rtx notes;
  4189.      rtx first, last;
  4190.      rtx orig_insn;
  4191. {
  4192.   rtx insn, note;
  4193.   rtx next;
  4194.   rtx orig_dest, temp;
  4195.   rtx set;
  4196.  
  4197.   /* Get and save the destination set by the original insn.  */
  4198.  
  4199.   orig_dest = single_set (orig_insn);
  4200.   if (orig_dest)
  4201.     orig_dest = SET_DEST (orig_dest);
  4202.  
  4203.   /* Move REG_NOTES from the original insn to where they now belong.  */
  4204.  
  4205.   for (note = notes; note; note = next)
  4206.     {
  4207.       next = XEXP (note, 1);
  4208.       switch (REG_NOTE_KIND (note))
  4209.     {
  4210.     case REG_DEAD:
  4211.     case REG_UNUSED:
  4212.       /* Move these notes from the original insn to the last new insn where
  4213.          the register is now set.  */
  4214.  
  4215.       for (insn = last; ; insn = PREV_INSN (insn))
  4216.         {
  4217.           if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
  4218.           && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
  4219.         {
  4220.           /* If this note refers to a multiple word hard register, it
  4221.              may have been split into several smaller hard register
  4222.              references, so handle it specially.  */
  4223.           temp = XEXP (note, 0);
  4224.           if (REG_NOTE_KIND (note) == REG_DEAD
  4225.               && GET_CODE (temp) == REG
  4226.               && REGNO (temp) < FIRST_PSEUDO_REGISTER
  4227.               && HARD_REGNO_NREGS (REGNO (temp), GET_MODE (temp)) > 1)
  4228.             split_hard_reg_notes (note, first, last, orig_insn);
  4229.           else
  4230.             {
  4231.               XEXP (note, 1) = REG_NOTES (insn);
  4232.               REG_NOTES (insn) = note;
  4233.             }
  4234.  
  4235.           /* Sometimes need to convert REG_UNUSED notes to REG_DEAD
  4236.              notes.  */
  4237.           /* ??? This won't handle multiple word registers correctly,
  4238.              but should be good enough for now.  */
  4239.           if (REG_NOTE_KIND (note) == REG_UNUSED
  4240.               && ! dead_or_set_p (insn, XEXP (note, 0)))
  4241.             PUT_REG_NOTE_KIND (note, REG_DEAD);
  4242.  
  4243.           /* The reg only dies in one insn, the last one that uses
  4244.              it.  */
  4245.           break;
  4246.         }
  4247.           /* It must die somewhere, fail it we couldn't find where it died.
  4248.  
  4249.          If this is a REG_UNUSED note, then it must be a temporary
  4250.          register that was not needed by this instantiation of the
  4251.          pattern, so we can safely ignore it.  */
  4252.           if (insn == first)
  4253.         {
  4254.           if (REG_NOTE_KIND (note) != REG_UNUSED)
  4255.             abort ();
  4256.  
  4257.           break;
  4258.         }
  4259.         }
  4260.       break;
  4261.  
  4262.     case REG_WAS_0:
  4263.       /* This note applies to the dest of the original insn.  Find the
  4264.          first new insn that now has the same dest, and move the note
  4265.          there.  */
  4266.  
  4267.       if (! orig_dest)
  4268.         abort ();
  4269.  
  4270.       for (insn = first; ; insn = NEXT_INSN (insn))
  4271.         {
  4272.           if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
  4273.           && (temp = single_set (insn))
  4274.           && rtx_equal_p (SET_DEST (temp), orig_dest))
  4275.         {
  4276.           XEXP (note, 1) = REG_NOTES (insn);
  4277.           REG_NOTES (insn) = note;
  4278.           /* The reg is only zero before one insn, the first that
  4279.              uses it.  */
  4280.           break;
  4281.         }
  4282.           /* It must be set somewhere, fail if we couldn't find where it
  4283.          was set.  */
  4284.           if (insn == last)
  4285.         abort ();
  4286.         }
  4287.       break;
  4288.  
  4289.     case REG_EQUAL:
  4290.     case REG_EQUIV:
  4291.       /* A REG_EQUIV or REG_EQUAL note on an insn with more than one
  4292.          set is meaningless.  Just drop the note.  */
  4293.       if (! orig_dest)
  4294.         break;
  4295.  
  4296.     case REG_NO_CONFLICT:
  4297.       /* These notes apply to the dest of the original insn.  Find the last
  4298.          new insn that now has the same dest, and move the note there.  */
  4299.  
  4300.       if (! orig_dest)
  4301.         abort ();
  4302.  
  4303.       for (insn = last; ; insn = PREV_INSN (insn))
  4304.         {
  4305.           if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
  4306.           && (temp = single_set (insn))
  4307.           && rtx_equal_p (SET_DEST (temp), orig_dest))
  4308.         {
  4309.           XEXP (note, 1) = REG_NOTES (insn);
  4310.           REG_NOTES (insn) = note;
  4311.           /* Only put this note on one of the new insns.  */
  4312.           break;
  4313.         }
  4314.  
  4315.           /* The original dest must still be set someplace.  Abort if we
  4316.          couldn't find it.  */
  4317.           if (insn == first)
  4318.         abort ();
  4319.         }
  4320.       break;
  4321.  
  4322.     case REG_LIBCALL:
  4323.       /* Move a REG_LIBCALL note to the first insn created, and update
  4324.          the corresponding REG_RETVAL note.  */
  4325.       XEXP (note, 1) = REG_NOTES (first);
  4326.       REG_NOTES (first) = note;
  4327.  
  4328.       insn = XEXP (note, 0);
  4329.       note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
  4330.       if (note)
  4331.         XEXP (note, 0) = first;
  4332.       break;
  4333.  
  4334.     case REG_RETVAL:
  4335.       /* Move a REG_RETVAL note to the last insn created, and update
  4336.          the corresponding REG_LIBCALL note.  */
  4337.       XEXP (note, 1) = REG_NOTES (last);
  4338.       REG_NOTES (last) = note;
  4339.  
  4340.       insn = XEXP (note, 0);
  4341.       note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
  4342.       if (note)
  4343.         XEXP (note, 0) = last;
  4344.       break;
  4345.  
  4346.     case REG_NONNEG:
  4347.       /* This should be moved to whichever instruction is a JUMP_INSN.  */
  4348.  
  4349.       for (insn = last; ; insn = PREV_INSN (insn))
  4350.         {
  4351.           if (GET_CODE (insn) == JUMP_INSN)
  4352.         {
  4353.           XEXP (note, 1) = REG_NOTES (insn);
  4354.           REG_NOTES (insn) = note;
  4355.           /* Only put this note on one of the new insns.  */
  4356.           break;
  4357.         }
  4358.           /* Fail if we couldn't find a JUMP_INSN.  */
  4359.           if (insn == first)
  4360.         abort ();
  4361.         }
  4362.       break;
  4363.  
  4364.     case REG_INC:
  4365.       /* This should be moved to whichever instruction now has the
  4366.          increment operation.  */
  4367.       abort ();
  4368.  
  4369.     case REG_LABEL:
  4370.       /* Should be moved to the new insn(s) which use the label.  */
  4371.       for (insn = first; insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
  4372.         if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
  4373.         && reg_mentioned_p (XEXP (note, 0), PATTERN (insn)))
  4374.           REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_LABEL,
  4375.                       XEXP (note, 0), REG_NOTES (insn));
  4376.       break;
  4377.  
  4378.     case REG_CC_SETTER:
  4379.     case REG_CC_USER:
  4380.       /* These two notes will never appear until after reorg, so we don't
  4381.          have to handle them here.  */
  4382.     default:
  4383.       abort ();
  4384.     }
  4385.     }
  4386.  
  4387.   /* Each new insn created, except the last, has a new set.  If the destination
  4388.      is a register, then this reg is now live across several insns, whereas
  4389.      previously the dest reg was born and died within the same insn.  To
  4390.      reflect this, we now need a REG_DEAD note on the insn where this
  4391.      dest reg dies.
  4392.  
  4393.      Similarly, the new insns may have clobbers that need REG_UNUSED notes.  */
  4394.  
  4395.   for (insn = first; insn != last; insn = NEXT_INSN (insn))
  4396.     {
  4397.       rtx pat;
  4398.       int i;
  4399.  
  4400.       pat = PATTERN (insn);
  4401.       if (GET_CODE (pat) == SET || GET_CODE (pat) == CLOBBER)
  4402.     new_insn_dead_notes (pat, insn, last, orig_insn);
  4403.       else if (GET_CODE (pat) == PARALLEL)
  4404.     {
  4405.       for (i = 0; i < XVECLEN (pat, 0); i++)
  4406.         if (GET_CODE (XVECEXP (pat, 0, i)) == SET
  4407.         || GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER)
  4408.           new_insn_dead_notes (XVECEXP (pat, 0, i), insn, last, orig_insn);
  4409.     }
  4410.     }
  4411.  
  4412.   /* If any insn, except the last, uses the register set by the last insn,
  4413.      then we need a new REG_DEAD note on that insn.  In this case, there
  4414.      would not have been a REG_DEAD note for this register in the original
  4415.      insn because it was used and set within one insn.
  4416.  
  4417.      There is no new REG_DEAD note needed if the last insn uses the register
  4418.      that it is setting.  */
  4419.  
  4420.   set = single_set (last);
  4421.   if (set)
  4422.     {
  4423.       rtx dest = SET_DEST (set);
  4424.  
  4425.       while (GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG
  4426.          || GET_CODE (dest) == STRICT_LOW_PART
  4427.          || GET_CODE (dest) == SIGN_EXTRACT)
  4428.     dest = XEXP (dest, 0);
  4429.  
  4430.       if (GET_CODE (dest) == REG
  4431.       && ! reg_overlap_mentioned_p (dest, SET_SRC (set)))
  4432.     {
  4433.       for (insn = PREV_INSN (last); ; insn = PREV_INSN (insn))
  4434.         {
  4435.           if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
  4436.           && reg_mentioned_p (dest, PATTERN (insn))
  4437.           && (set = single_set (insn)))
  4438.         {
  4439.           rtx insn_dest = SET_DEST (set);
  4440.  
  4441.           while (GET_CODE (insn_dest) == ZERO_EXTRACT
  4442.              || GET_CODE (insn_dest) == SUBREG
  4443.              || GET_CODE (insn_dest) == STRICT_LOW_PART
  4444.              || GET_CODE (insn_dest) == SIGN_EXTRACT)
  4445.             insn_dest = XEXP (insn_dest, 0);
  4446.  
  4447.           if (insn_dest != dest)
  4448.             {
  4449.               note = rtx_alloc (EXPR_LIST);
  4450.               PUT_REG_NOTE_KIND (note, REG_DEAD);
  4451.               XEXP (note, 0) = dest;
  4452.               XEXP (note, 1) = REG_NOTES (insn);
  4453.               REG_NOTES (insn) = note;
  4454.               /* The reg only dies in one insn, the last one
  4455.              that uses it.  */
  4456.               break;
  4457.             }
  4458.         }
  4459.           if (insn == first)
  4460.         break;
  4461.         }
  4462.     }
  4463.     }
  4464.  
  4465.   /* If the original dest is modifying a multiple register target, and the
  4466.      original instruction was split such that the original dest is now set
  4467.      by two or more SUBREG sets, then the split insns no longer kill the
  4468.      destination of the original insn.
  4469.  
  4470.      In this case, if there exists an instruction in the same basic block,
  4471.      before the split insn, which uses the original dest, and this use is
  4472.      killed by the original insn, then we must remove the REG_DEAD note on
  4473.      this insn, because it is now superfluous.
  4474.  
  4475.      This does not apply when a hard register gets split, because the code
  4476.      knows how to handle overlapping hard registers properly.  */
  4477.   if (orig_dest && GET_CODE (orig_dest) == REG)
  4478.     {
  4479.       int found_orig_dest = 0;
  4480.       int found_split_dest = 0;
  4481.  
  4482.       for (insn = first; ; insn = NEXT_INSN (insn))
  4483.     {
  4484.       set = single_set (insn);
  4485.       if (set)
  4486.         {
  4487.           if (GET_CODE (SET_DEST (set)) == REG
  4488.           && REGNO (SET_DEST (set)) == REGNO (orig_dest))
  4489.         {
  4490.           found_orig_dest = 1;
  4491.           break;
  4492.         }
  4493.           else if (GET_CODE (SET_DEST (set)) == SUBREG
  4494.                && SUBREG_REG (SET_DEST (set)) == orig_dest)
  4495.         {
  4496.           found_split_dest = 1;
  4497.           break;
  4498.         }
  4499.         }
  4500.  
  4501.       if (insn == last)
  4502.         break;
  4503.     }
  4504.  
  4505.       if (found_split_dest)
  4506.     {
  4507.       /* Search backwards from FIRST, looking for the first insn that uses
  4508.          the original dest.  Stop if we pass a CODE_LABEL or a JUMP_INSN.
  4509.          If we find an insn, and it has a REG_DEAD note, then delete the
  4510.          note.  */
  4511.  
  4512.       for (insn = first; insn; insn = PREV_INSN (insn))
  4513.         {
  4514.           if (GET_CODE (insn) == CODE_LABEL
  4515.           || GET_CODE (insn) == JUMP_INSN)
  4516.         break;
  4517.           else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
  4518.                && reg_mentioned_p (orig_dest, insn))
  4519.         {
  4520.           note = find_regno_note (insn, REG_DEAD, REGNO (orig_dest));
  4521.           if (note)
  4522.             remove_note (insn, note);
  4523.         }
  4524.         }
  4525.     }
  4526.       else if (! found_orig_dest)
  4527.     {
  4528.       /* This should never happen.  */
  4529.       abort ();
  4530.     }
  4531.     }
  4532.  
  4533.   /* Update reg_n_sets.  This is necessary to prevent local alloc from
  4534.      converting REG_EQUAL notes to REG_EQUIV when splitting has modified
  4535.      a reg from set once to set multiple times.  */
  4536.  
  4537.   {
  4538.     rtx x = PATTERN (orig_insn);
  4539.     RTX_CODE code = GET_CODE (x);
  4540.  
  4541.     if (code == SET || code == CLOBBER)
  4542.       update_n_sets (x, -1);
  4543.     else if (code == PARALLEL)
  4544.       {
  4545.     int i;
  4546.     for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
  4547.       {
  4548.         code = GET_CODE (XVECEXP (x, 0, i));
  4549.         if (code == SET || code == CLOBBER)
  4550.           update_n_sets (XVECEXP (x, 0, i), -1);
  4551.       }
  4552.       }
  4553.  
  4554.     for (insn = first; ; insn = NEXT_INSN (insn))
  4555.       {
  4556.     x = PATTERN (insn);
  4557.     code = GET_CODE (x);
  4558.  
  4559.     if (code == SET || code == CLOBBER)
  4560.       update_n_sets (x, 1);
  4561.     else if (code == PARALLEL)
  4562.       {
  4563.         int i;
  4564.         for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
  4565.           {
  4566.         code = GET_CODE (XVECEXP (x, 0, i));
  4567.         if (code == SET || code == CLOBBER)
  4568.           update_n_sets (XVECEXP (x, 0, i), 1);
  4569.           }
  4570.       }
  4571.  
  4572.     if (insn == last)
  4573.       break;
  4574.       }
  4575.   }
  4576. }
  4577.  
  4578. /* The one entry point in this file.  DUMP_FILE is the dump file for
  4579.    this pass.  */
  4580.  
  4581. void
  4582. schedule_insns (dump_file)
  4583.      FILE *dump_file;
  4584. {
  4585.   int max_uid = MAX_INSNS_PER_SPLIT * (get_max_uid () + 1);
  4586.   int b;
  4587.   rtx insn;
  4588.  
  4589.   /* Taking care of this degenerate case makes the rest of
  4590.      this code simpler.  */
  4591.   if (n_basic_blocks == 0)
  4592.     return;
  4593.  
  4594.   /* Create an insn here so that we can hang dependencies off of it later.  */
  4595.   sched_before_next_call
  4596.     = gen_rtx (INSN, VOIDmode, 0, NULL_RTX, NULL_RTX,
  4597.            NULL_RTX, 0, NULL_RTX, 0);
  4598.  
  4599.   /* Initialize the unused_*_lists.  We can't use the ones left over from
  4600.      the previous function, because gcc has freed that memory.  We can use
  4601.      the ones left over from the first sched pass in the second pass however,
  4602.      so only clear them on the first sched pass.  The first pass is before
  4603.      reload if flag_schedule_insns is set, otherwise it is afterwards.  */
  4604.  
  4605.   if (reload_completed == 0 || ! flag_schedule_insns)
  4606.     {
  4607.       unused_insn_list = 0;
  4608.       unused_expr_list = 0;
  4609.     }
  4610.  
  4611.   /* We create no insns here, only reorder them, so we
  4612.      remember how far we can cut back the stack on exit.  */
  4613.  
  4614.   /* Allocate data for this pass.  See comments, above,
  4615.      for what these vectors do.  */
  4616.   insn_luid = (int *) alloca (max_uid * sizeof (int));
  4617.   insn_priority = (int *) alloca (max_uid * sizeof (int));
  4618.   insn_tick = (int *) alloca (max_uid * sizeof (int));
  4619.   insn_costs = (short *) alloca (max_uid * sizeof (short));
  4620.   insn_units = (short *) alloca (max_uid * sizeof (short));
  4621.   insn_blockage = (unsigned int *) alloca (max_uid * sizeof (unsigned int));
  4622.   insn_ref_count = (int *) alloca (max_uid * sizeof (int));
  4623.  
  4624.   if (reload_completed == 0)
  4625.     {
  4626.       sched_reg_n_deaths = (short *) alloca (max_regno * sizeof (short));
  4627.       sched_reg_n_calls_crossed = (int *) alloca (max_regno * sizeof (int));
  4628.       sched_reg_live_length = (int *) alloca (max_regno * sizeof (int));
  4629.       bb_dead_regs = (regset) alloca (regset_bytes);
  4630.       bb_live_regs = (regset) alloca (regset_bytes);
  4631.       bzero ((char *) sched_reg_n_calls_crossed, max_regno * sizeof (int));
  4632.       bzero ((char *) sched_reg_live_length, max_regno * sizeof (int));
  4633.       bcopy ((char *) reg_n_deaths, (char *) sched_reg_n_deaths,
  4634.          max_regno * sizeof (short));
  4635.       init_alias_analysis ();
  4636.     }
  4637.   else
  4638.     {
  4639.       sched_reg_n_deaths = 0;
  4640.       sched_reg_n_calls_crossed = 0;
  4641.       sched_reg_live_length = 0;
  4642.       bb_dead_regs = 0;
  4643.       bb_live_regs = 0;
  4644.       if (! flag_schedule_insns)
  4645.     init_alias_analysis ();
  4646.     }
  4647.  
  4648.   if (write_symbols != NO_DEBUG)
  4649.     {
  4650.       rtx line;
  4651.  
  4652.       line_note = (rtx *) alloca (max_uid * sizeof (rtx));
  4653.       bzero ((char *) line_note, max_uid * sizeof (rtx));
  4654.       line_note_head = (rtx *) alloca (n_basic_blocks * sizeof (rtx));
  4655.       bzero ((char *) line_note_head, n_basic_blocks * sizeof (rtx));
  4656.  
  4657.       /* Determine the line-number at the start of each basic block.
  4658.      This must be computed and saved now, because after a basic block's
  4659.      predecessor has been scheduled, it is impossible to accurately
  4660.      determine the correct line number for the first insn of the block.  */
  4661.      
  4662.       for (b = 0; b < n_basic_blocks; b++)
  4663.     for (line = basic_block_head[b]; line; line = PREV_INSN (line))
  4664.       if (GET_CODE (line) == NOTE && NOTE_LINE_NUMBER (line) > 0)
  4665.         {
  4666.           line_note_head[b] = line;
  4667.           break;
  4668.         }
  4669.     }
  4670.  
  4671.   bzero ((char *) insn_luid, max_uid * sizeof (int));
  4672.   bzero ((char *) insn_priority, max_uid * sizeof (int));
  4673.   bzero ((char *) insn_tick, max_uid * sizeof (int));
  4674.   bzero ((char *) insn_costs, max_uid * sizeof (short));
  4675.   bzero ((char *) insn_units, max_uid * sizeof (short));
  4676.   bzero ((char *) insn_blockage, max_uid * sizeof (unsigned int));
  4677.   bzero ((char *) insn_ref_count, max_uid * sizeof (int));
  4678.  
  4679.   /* Schedule each basic block, block by block.  */
  4680.  
  4681.   /* ??? Add a NOTE after the last insn of the last basic block.  It is not
  4682.      known why this is done.  */
  4683.  
  4684.   insn = basic_block_end[n_basic_blocks-1];
  4685.   if (NEXT_INSN (insn) == 0
  4686.       || (GET_CODE (insn) != NOTE
  4687.       && GET_CODE (insn) != CODE_LABEL
  4688.       /* Don't emit a NOTE if it would end up between an unconditional
  4689.          jump and a BARRIER.  */
  4690.       && ! (GET_CODE (insn) == JUMP_INSN
  4691.         && GET_CODE (NEXT_INSN (insn)) == BARRIER)))
  4692.     emit_note_after (NOTE_INSN_DELETED, basic_block_end[n_basic_blocks-1]);
  4693.  
  4694.   for (b = 0; b < n_basic_blocks; b++)
  4695.     {
  4696.       rtx insn, next;
  4697.  
  4698.       note_list = 0;
  4699.  
  4700.       for (insn = basic_block_head[b]; ; insn = next)
  4701.     {
  4702.       rtx prev;
  4703.       rtx set;
  4704.  
  4705.       /* Can't use `next_real_insn' because that
  4706.          might go across CODE_LABELS and short-out basic blocks.  */
  4707.       next = NEXT_INSN (insn);
  4708.       if (GET_CODE (insn) != INSN)
  4709.         {
  4710.           if (insn == basic_block_end[b])
  4711.         break;
  4712.  
  4713.           continue;
  4714.         }
  4715.  
  4716.       /* Don't split no-op move insns.  These should silently disappear
  4717.          later in final.  Splitting such insns would break the code
  4718.          that handles REG_NO_CONFLICT blocks.  */
  4719.       set = single_set (insn);
  4720.       if (set && rtx_equal_p (SET_SRC (set), SET_DEST (set)))
  4721.         {
  4722.           if (insn == basic_block_end[b])
  4723.         break;
  4724.  
  4725.           /* Nops get in the way while scheduling, so delete them now if
  4726.          register allocation has already been done.  It is too risky
  4727.          to try to do this before register allocation, and there are
  4728.          unlikely to be very many nops then anyways.  */
  4729.           if (reload_completed)
  4730.         {
  4731.           PUT_CODE (insn, NOTE);
  4732.           NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
  4733.           NOTE_SOURCE_FILE (insn) = 0;
  4734.         }
  4735.  
  4736.           continue;
  4737.         }
  4738.  
  4739.       /* Split insns here to get max fine-grain parallelism.  */
  4740.       prev = PREV_INSN (insn);
  4741.       if (reload_completed == 0)
  4742.         {
  4743.           rtx last, first = PREV_INSN (insn);
  4744.           rtx notes = REG_NOTES (insn);
  4745.  
  4746.           last = try_split (PATTERN (insn), insn, 1);
  4747.           if (last != insn)
  4748.         {
  4749.           /* try_split returns the NOTE that INSN became.  */
  4750.           first = NEXT_INSN (first);
  4751.           update_flow_info (notes, first, last, insn);
  4752.  
  4753.           PUT_CODE (insn, NOTE);
  4754.           NOTE_SOURCE_FILE (insn) = 0;
  4755.           NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
  4756.           if (insn == basic_block_head[b])
  4757.             basic_block_head[b] = first;
  4758.           if (insn == basic_block_end[b])
  4759.             {
  4760.               basic_block_end[b] = last;
  4761.               break;
  4762.             }
  4763.         }
  4764.         }
  4765.  
  4766.       if (insn == basic_block_end[b])
  4767.         break;
  4768.     }
  4769.  
  4770.       schedule_block (b, dump_file);
  4771.  
  4772. #ifdef USE_C_ALLOCA
  4773.       alloca (0);
  4774. #endif
  4775.     }
  4776.  
  4777.   /* Reposition the prologue and epilogue notes in case we moved the
  4778.      prologue/epilogue insns.  */
  4779.   if (reload_completed)
  4780.     reposition_prologue_and_epilogue_notes (get_insns ());
  4781.  
  4782.   if (write_symbols != NO_DEBUG)
  4783.     {
  4784.       rtx line = 0;
  4785.       rtx insn = get_insns ();
  4786.       int active_insn = 0;
  4787.       int notes = 0;
  4788.  
  4789.       /* Walk the insns deleting redundant line-number notes.  Many of these
  4790.      are already present.  The remainder tend to occur at basic
  4791.      block boundaries.  */
  4792.       for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
  4793.     if (GET_CODE (insn) == NOTE && NOTE_LINE_NUMBER (insn) > 0)
  4794.       {
  4795.         /* If there are no active insns following, INSN is redundant.  */
  4796.         if (active_insn == 0)
  4797.           {
  4798.         notes++;
  4799.         NOTE_SOURCE_FILE (insn) = 0;
  4800.         NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
  4801.           }
  4802.         /* If the line number is unchanged, LINE is redundant.  */
  4803.         else if (line
  4804.              && NOTE_LINE_NUMBER (line) == NOTE_LINE_NUMBER (insn)
  4805.              && NOTE_SOURCE_FILE (line) == NOTE_SOURCE_FILE (insn))
  4806.           {
  4807.         notes++;
  4808.         NOTE_SOURCE_FILE (line) = 0;
  4809.         NOTE_LINE_NUMBER (line) = NOTE_INSN_DELETED;
  4810.         line = insn;
  4811.           }
  4812.         else
  4813.           line = insn;
  4814.         active_insn = 0;
  4815.       }
  4816.     else if (! ((GET_CODE (insn) == NOTE
  4817.              && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED)
  4818.             || (GET_CODE (insn) == INSN
  4819.             && (GET_CODE (PATTERN (insn)) == USE
  4820.                 || GET_CODE (PATTERN (insn)) == CLOBBER))))
  4821.       active_insn++;
  4822.  
  4823.       if (dump_file && notes)
  4824.     fprintf (dump_file, ";; deleted %d line-number notes\n", notes);
  4825.     }
  4826.  
  4827.   if (reload_completed == 0)
  4828.     {
  4829.       int regno;
  4830.       for (regno = 0; regno < max_regno; regno++)
  4831.     if (sched_reg_live_length[regno])
  4832.       {
  4833.         if (dump_file)
  4834.           {
  4835.         if (reg_live_length[regno] > sched_reg_live_length[regno])
  4836.           fprintf (dump_file,
  4837.                ";; register %d life shortened from %d to %d\n",
  4838.                regno, reg_live_length[regno],
  4839.                sched_reg_live_length[regno]);
  4840.         /* Negative values are special; don't overwrite the current
  4841.            reg_live_length value if it is negative.  */
  4842.         else if (reg_live_length[regno] < sched_reg_live_length[regno]
  4843.              && reg_live_length[regno] >= 0)
  4844.           fprintf (dump_file,
  4845.                ";; register %d life extended from %d to %d\n",
  4846.                regno, reg_live_length[regno],
  4847.                sched_reg_live_length[regno]);
  4848.  
  4849.         if (! reg_n_calls_crossed[regno]
  4850.             && sched_reg_n_calls_crossed[regno])
  4851.           fprintf (dump_file,
  4852.                ";; register %d now crosses calls\n", regno);
  4853.         else if (reg_n_calls_crossed[regno]
  4854.              && ! sched_reg_n_calls_crossed[regno]
  4855.              && reg_basic_block[regno] != REG_BLOCK_GLOBAL)
  4856.           fprintf (dump_file,
  4857.                ";; register %d no longer crosses calls\n", regno);
  4858.  
  4859.           }
  4860.         /* Negative values are special; don't overwrite the current
  4861.            reg_live_length value if it is negative.  */
  4862.         if (reg_live_length[regno] >= 0)
  4863.           reg_live_length[regno] = sched_reg_live_length[regno];
  4864.  
  4865.         /* We can't change the value of reg_n_calls_crossed to zero for
  4866.            pseudos which are live in more than one block.
  4867.  
  4868.            This is because combine might have made an optimization which
  4869.            invalidated basic_block_live_at_start and reg_n_calls_crossed,
  4870.            but it does not update them.  If we update reg_n_calls_crossed
  4871.            here, the two variables are now inconsistent, and this might
  4872.            confuse the caller-save code into saving a register that doesn't
  4873.            need to be saved.  This is only a problem when we zero calls
  4874.            crossed for a pseudo live in multiple basic blocks.
  4875.  
  4876.            Alternatively, we could try to correctly update basic block live
  4877.            at start here in sched, but that seems complicated.  */
  4878.         if (sched_reg_n_calls_crossed[regno]
  4879.         || reg_basic_block[regno] != REG_BLOCK_GLOBAL)
  4880.           reg_n_calls_crossed[regno] = sched_reg_n_calls_crossed[regno];
  4881.       }
  4882.     }
  4883. }
  4884. #endif /* INSN_SCHEDULING */
  4885.